Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate code to null safety in Dart

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.

like image 433
Paresh Mangukiya Avatar asked Mar 16 '21 02:03

Paresh Mangukiya


People also ask

How do I enable null safety in Dart?

To allow nullable type in Dart, you must add a “?” (quotation mark) at the type identifier. This action tells dart that this value is nullable. It may have or have not a value.

Can int be null in Dart?

You can specify that a type of a variable is nullable (e.g. int? i ), and only then can they contain either a null or a value of the defined type.

WHAT IS null value in Dart?

The keyword for Null in the programming language Dart is null. Null means a variable which has no values assign ever and the variable is initialized with nothing like.


1 Answers

  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

like image 167
Javeed Ishaq Avatar answered Oct 29 '22 22:10

Javeed Ishaq