Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Null-Safety in Flutter?

I tried to use null safety, but it's giving me this error:

This requires the 'non-nullable' language feature to be enabled. Try updating your pubspec.yaml to set the minimum SDK constraint to 2.10.0 or higher, and running 'pub get'.

I changed my Dart SDK constraint from 2.7.0 to 2.10.0, but it's still showing this error.

enter image description here

Also, I upgraded my Dart and Flutter SDK:

dart-sdk v2.10.2 is the latest version available based on your source(s).

Flutter (Channel stable, 1.22.3, ...

like image 356
Abdallah El-Rashedy Avatar asked Oct 31 '20 11:10

Abdallah El-Rashedy


People also ask

How do I run a null safety Flutter?

dart" at top of IDE, and click on "Edit Configuration". Note: You need to add Flutter and Dart plugin on android studio, otherwise this menu is not available. Copy: --no-sound-null-safety and add into "additional run args". Run your app with "Run/Play" Button or from "Run" Menu.


2 Answers

Null safety is no longer an experiment as of Dart 2.12. It is now easy to enable.

Enabling null safety

Starting with the first Dart 2.12 versions, types will be non-nullable by default. So you just need to change your SDK constraint:

environment:   sdk: ">=2.12.0 <3.0.0" 

Learn more about "Enabling null safety" on dart.dev.

like image 121
creativecreatorormaybenot Avatar answered Sep 29 '22 02:09

creativecreatorormaybenot


To Enable null safety,

  1. Check Latest Dart Version(It should be Dart 2.12 or later:)

    dart --version 
  2. Update the dart version, the above point not satisfied using the command.

    dart pub upgrade --null-safety  dart pub get 
  3. Run the below command to know what are libs in your project needs to be upgraded to the latest null safety.

    dart pub outdated --mode=null-safety 

The latest column shows the current version if it's in green that means dependency implemented null safety features if it red then the dependency owner needs to implement that.

enter image description here

  1. Finally, run dart migration command which performs null safety migration on existing project(Existing project)

    dart migrate 
  2. If your package is ready to migrate, then the tool produces a line like the following:

    View the migration suggestions by visiting:

    http://127.0.0.1:60278/Users/you/project/mypkg.console-simple?authToken=Xfz0jvpyeMI%3D

Note: Even after running upgrade --null-safety command, you see the latest column section in red, which means a particular dependency hasn't supported null safety yet, so that means you cannot migrate the project.

for detail read: https://dart.dev/null-safety/migration-guide

Good blog on Implementation of Null Safety: https://medium.com/flutterworld/flutter-null-safety-5d20012c2441

like image 27
Jitesh Mohite Avatar answered Sep 29 '22 02:09

Jitesh Mohite