Migrating to null safety in Flutter/Dart

If you are starting with Flutter, just like me, and end up having used dependency packages with versions which do not support null safety checks then you are most likely to come across with errors such as the following when you do build.

error GBA2BE6E0: A library can't opt out of null safety by default, when using sound null safety.

Of course you could still run the application by opting out of “sound null safety” checks using the folllowing run command flags.

flutter run --no-sound-null-safety

But in this post, we will look into how to check if your dependencies have newer versions which have null safety checks enabled.If you application needs permits, then you could update the versions to ensure your applications support sound null safety checks.

To check the dependency status of your application, use the following command.

dart pub outdated --mode=null-safety

This would return the list of dependencies and their migration status. If any of the dependency which currently do not support null-safety, has a newer version which supports it, it would be also displayed as shown in the output below.

Resolving...
Showing dependencies that are currently not opted in to null-safety.
[x] indicates versions without null safety support.
[+] indicates versions opting in to null safety.

Computing null safety support...
Package Name  Current  Upgradable  Resolvable  Latest

direct dependencies:
grpc          x2.9.0   x2.9.0      +3.0.2      +3.0.2
http          x0.12.2  x0.12.2     +0.13.5     +0.13.5

dev_dependencies: all support null safety.

2  dependencies are constrained to versions that are older than a resolvable version.
To update these dependencies, edit pubspec.yaml, or run `dart pub upgrade --null-safety`.

In the above output, you can notice that the grpc and http dependencies were using versions which were not currently support null-safety. However, the commmand also indicated via the Resolvable version a recent version of the dependency which support null safety. If you are application needs are not affected by the upgrade to the newer version, you can now head over to the pubspec.yaml and update the dependency versions.

Once the pubspec.yaml is updated, run the command again to ensure the dependencies are now showing as support.

> dart pub outdated --mode=null-safety
Resolving...
Showing dependencies that are currently not opted in to null-safety.
[x] indicates versions without null safety support.
[+] indicates versions opting in to null safety.

Computing null safety support...
All your dependencies declare support for null-safety.

You can now run your flutter build command to ensure no null-safety warnings are reported.

You could read more on Sound Null Safety checks in official page.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s