How to enable Null-Safety in Flutter?
Asked Answered
L

8

60

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, ...

Leavings answered 31/10, 2020 at 11:9 Comment(0)
T
52

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

Tabatha answered 15/3, 2021 at 11:0 Comment(0)
B
74

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.

Balk answered 2/12, 2020 at 23:2 Comment(6)
It is now stable (Flutter 2.0.0)Matz
Getting error: [chatflutter] flutter pub get Running "flutter pub get" in chatflutter... The current Dart SDK version is 2.12.0-133.7.beta. Because chatflutter requires SDK version >=2.12.0 <3.0.0, version solving failed. pub get failed (1; Because chatflutter requires SDK version >=2.12.0 <3.0.0, version solving failed.) exit code 1Watermark
@Watermark Please make sure to run flutter upgrade :) Dart 2.12.0 is available as of Flutter 2.0.0.Balk
I have already developed the application and if I go with Dart 2.12 version with null safety feature then it will throw me back 10 days to solve all the issues are raised after applying Dart 2.12 version with null safety. BTW I have updated flutter version to 2.0.0 and made flutter ui changes in my app. I made a copy of my application and applied Dart 2.12 version then fixed Dart 2.12 updates related issues in my app and wasted 11-12 hours which was waste of time because many packages are not compatible with Dart 2.12 null safety feature, then I rolled back my application today. Thanks a lot.Watermark
Dart is also saying that WAIT for the packages that you depend on to migrate, so not good to migrate to Dart 2.12 with null safety feature. dart.dev/null-safety/migration-guideWatermark
I think it's actually good to migrate as soon as you've got the green light, @Watermark . Like you said, you just have to wait for your dependencies before doing so. 🙃Chandelle
T
52

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

Tabatha answered 15/3, 2021 at 11:0 Comment(0)
R
10

After Flutter 2.0.0 Stable u can run in app root folder:

dart migrate --apply-changes
Rillet answered 12/3, 2021 at 11:39 Comment(0)
G
8

Following the Dart documentation I was able to enable null safety in Flutter with these steps:

First add analysis_options.yaml:

analyzer:
  enable-experiment:
    - non-nullable

Then move to the dev channel and upgrade:

flutter channel dev
flutter upgrade

Change the sdk in pubspec.yaml

environment:
  sdk: ">=2.11.0-213.0.dev <2.12.0"

Clean the project:

flutter clean
flutter pub get

Restart the IDE (VS Code in my case).

And then it was working fine.

Gelya answered 31/10, 2020 at 15:56 Comment(1)
Now, NNBD is no longer an experiment. See my answer.Balk
B
7

This can happen when you upgrade the Flutter version you're using.

Try adding the following to the analysis_options.yml

analyzer:
    - enable-experiment:
        - non-nullable

Then, try cleaning and upgrading the project dependencies again.
To do that, you can use the following commands:

flutter clean
flutter packages pub upgrade
flutter pub run build_runner build

Finally, restart your IDE.

P.s. By the way, make sure that the sdk you're using is compatible with your Flutter version

Broadtail answered 31/10, 2020 at 11:35 Comment(3)
do I must to import something?Leavings
Can you try restarting your ide? https://mcmap.net/q/330258/-how-can-i-enable-flutter-dart-language-experimentsBroadtail
@AbdallahEl-Rashedy This now does not require the enable-experiment anymore; see my answer.Balk
A
3
  1. Set the lower Dart SDK constraint to 2.12 in your pubspec.yaml file.

    environment:
      sdk: ">=2.12.0 <3.0.0"
    
  2. Check if all the packages of your app are migrated to null safety.

    dart pub outdated --mode=null-safety
    
  3. If they are migrated, update all the packages to their null safe variant.

    dart pub upgrade --null-safety 
    dart pub get
    
  4. Start the migration process.

    dart migrate
    

    If you're happy with the changes, apply them using:

    dart migrate --apply-changes  
    
Antarctic answered 29/8, 2021 at 12:48 Comment(0)
P
2

I had done all the above and yet for some reason the analyzer still gave an error about enabling null safety. What fixed it for me was running pub upgrade instead of pub get. I'm not even going to try to find out why, I'm just going to get back to work!

Postglacial answered 10/12, 2020 at 15:54 Comment(1)
" I'm not even going to try to find out why, I'm just going to get back to work!" I am taking this as a life lesson after trying to fix this issue :)Lamed
Y
0

I confirm that evenIn 2022 it still works doing this...

Environment: SDK: ">=2.12.0 <3.0.0"

Yocum answered 18/11, 2022 at 1:45 Comment(1)
I am having exactly same issue, my Dart & Flutter are both on latest versions. Pubspec.yaml is correct. Android Studio has been restarted. Flutter pub get has been run.Indiana

© 2022 - 2024 — McMap. All rights reserved.