Skip to content

Latest commit

 

History

History
109 lines (73 loc) · 3.02 KB

migration_v2.md

File metadata and controls

109 lines (73 loc) · 3.02 KB

Migration guide for version 2.x.x

The collection package, that is maintained by the Dart team, adopted (1.15.0-nullsafety.4) several extension methods that causes problems with Supercharged extension methods.

The release 2.x.x of supercharged (or supercharged_dart) addresses these changes. This document will guide you through the migration process.

Renaming

In order to keep compile issues as low as possible, Supercharged renamed all affected methods by adding the suffix SC to the method name:

  • sortedBy renamed to sortedBySC
  • sortedByNum renamed to sortedByNumSC
  • sortedByString renamed to sortedByStringSC
  • sum renamed to sumSC
  • average renamed to averageSC
  • firstOrNull renamed to firstOrNullSC
  • lastOrNull renamed to lastOrNullSC
  • forEachIndexed renamed to forEachIndexedSC
  • mapIndexed renamed to mapIndexedSC

In a first step of migration you just need to identify these methods in your code by looking for compile errors. Then add the suffix SC to the method name. It will solve the compile error and mark the used method as deprecated.

Now you are free to stop here and focus on other issues demanded by the general null-safety migration process.

Adopting Dart's native methods

Once your application compiles just perfectly, you can focus on further adoption of Dart's native methods.

You need to import the collection package that is maintained by the Dart team:

import 'package:collection/collection.dart';

sortedBy / sortedByNum / sortedByString

Instead of comparing two values inside a function, you just return a value of a single item. You also need to pass in a type, that's implementing a comparator, so it knows the natural order.

[3, 1, 5, 9, 7].sortedBySC((a, b) => a.compareTo(b))

can be migrated into

[3, 1, 5, 9, 7].sortedBy<num>((a) => a)

Working with Strings is quite the same:

['b', 'c', 'a'].sortedBy<String>((s) => s)

sum / average

The dart equivalent is nearly identical. Just remove the parenthesis since Dart uses only a getter method.

[1, 2, 3].sumSC()
[1, 2, 3].averageSC()

can be migrated into

[1, 2, 3].sum
[1, 2, 3].average

firstOrNull / lastOrNull

The dart equivalent is nearly identical. Just remove the parenthesis since Dart uses only a getter method.

['a', 'b'].firstOrNullSC()
['a', 'b'].lastOrNullSC()

can be migrated into

['a', 'b'].firstOrNull
['a', 'b'].lastOrNull

forEachIndex

The method signature of Dart's native method is identical to the one in Supercharged.

[1, 2, 3].forEachIndexed((index, value) {
  // ...    
});

mapIndexed

Be cautious on this one. The name is identical, but the positions of the parameters are swapped.

[1, 2, 3].mapIndexedSC((element, index) => /* ... */)

can be migrated into

[1, 2, 3].mapIndexedSC((index, element) => /* ... */)