Sound null safety is available in Dart 2.12 and Flutter 2.
In reading through the migration documentation, it appears the migration tool may pose as much time and effort as migrating by hand. And being I’m in this to learn the code, migrating by hand is the path I’ve opted for. I’m currently on Step 2.4 of the Migration Guide.
Because each of my reminder apps is setup with a similar code base, I also decided to migrate all three apps at the same time. Thus far I’ve got one app down to 500 critical errors, while the other two are down to 396 and 393.
Mobile Apps | Critical | Warn | Info |
---|---|---|---|
B4-I-Go | 500 | 10 | 64 |
Hungry-on_Hand | 396 | 8 | 56 |
Xpired To Be | 393 | 10 | 58 |
Having started at over 600 critical errors on the B4-I-Go app, the migration thus far has been tedious, but informative and enlightening—to both Dart, and my app’s own code.
I’m sure a pattern of migrating will emerge, but currently the most time-consuming aspect comes in having to analyze the context of each error to determine if a variable can be null, or if it will never be null. It seems a small distinction between the two when reading about them, but in context, the decision seems quite important for each case.
? | |=> |
|
|
||
! | |=> |
|
|
||
|
The other two prominent null safe keywords are required and late. While required was introduced before I began learning Flutter via the @required annotation, which is easily remedied by removing the `@`, I’ve come across ample variables that have made good of the new late modifier, which simply indicates the variable will be initialized later.
⚠ If you fail to initialize a late variable, a runtime error occurs when the variable is used.
Fortunately, should errors prevail when the migration is complete, I should also get a good look at Flutter’s new debugging updates.
Happy migrating!
– Keith | https://keithdc.com
Converting nullable dynamic Lists inside Maps
Map<String, List<dynamic>> _statusObj = {‘message’: [‘Hello!’]};
Map<String, List<dynamic>> _statusObj = {};
List<String> _alertMsgs = List<String>.from(_statusObj[“message”]?.toList() ?? <String>[]);
- DartPad (runs the ^^^ Gist): https://dartpad.dev/f329655032b7b189dfdd9e82e97010e3
Keith D Commiskey
https://keithdc.com
The overall migration to null safety doing three similar apps at the same time took 3+ days.
Starting at 500 critical errors on the B4-I-Go app, not counting the first 157 critical errors…
From 157 – 341
@ 184 crits | Was 4:15 min per crit.
From 341 – 500
@ 159 crits | Was 3:10 min per crit.
This included research and analysis, general debugging, and breaks.
Towards the end some patterns did emerge, and the last 91 critical errors took less than 2 hours at about 1:15 min per crit.