How to migrate code to null safety in Dart

Issue

Recently Flutter team announced the Dart language now supports sound null safety! and is available in Dart 2.12 and Flutter 2. I want to migrate the app code to null safety.

How to migrate my packages to null safety,

It would be great if someone could give instructions about migrating to null-safety.

Solution

  1. Run flutter upgrade in the terminal to upgrade Flutter

  2. Run dart migrate to run the dart migration tool

  3. Solve all errors which the migration tool shows

  4. Run flutter pub outdated --mode=null-safety to print all outdated
    packages

    enter image description here

You can see if the packages you depend upon support null-safety.

  1. Run flutter pub upgrade --null-safety to upgrade all packages
    automatically
  2. Check the code for errors and solve them
  3. Run dart migrate again and it should now be successful. Follow the
    link to check out the proposed changes
  4. Press the "Apply Migration" button
  5. Check the code for errors again and fix them

Congratulations, when finished you should now be able to run the app with sound null-safety.

Run flutter run in the command line and the application should run with the command line displaying:

enter image description here

Answered By – Javeed Ishaq

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *