user3612643
user3612643

Reputation: 5772

Error: This requires the 'non-nullable' experiment to be enabled

I am playing around with non-nullable types and added this to my analysis_options.yaml:

analyzer:
  enable-experiment:
    - non-nullable

I have a code-generator that is utilising the nullability extension. Visual Code is fine with my code.

Now, I try to run:

flutter packages pub run build_runner watch 

I get this error message:

[SEVERE] Failed to snapshot build script .dart_tool/build/entrypoint/build.dart.
This is likely caused by a misconfigured builder definition.
[SEVERE] xyz.dart:95:7: Error: This requires the 'non-nullable' experiment to be enabled.Try enabling this experiment by adding it to the command line when compiling and running.

How can I pass --enable-experiment:non-nullable to flutter packages pub run?

The same happens if I run:

flutter build ios

I get the error message:

  lib/main.dart:61:26: Error: This requires the 'non-nullable' experiment to be enabled.

    Try enabling this experiment by adding it to the command line when compiling and running.

So, same question: How can I pass --enable-experiment:non-nullable to flutter build?

Upvotes: 50

Views: 58408

Answers (6)

Rahman Developer
Rahman Developer

Reputation: 1

The problem will be solved easily, Do these things | rAhMaN

  1. If you have upgraded Flutter then downgrade your flutter sdk use the command in flutter_console.bat "flutter downgrade" it reverses your SDK package.
  2. Again in which after completion run the command "flutter pub upgrade"
  3. Check your IDE and run "flutter pub get"
  4. If the again give issue then check your dependency that is highly recommended for your project like all Firebase packages, http, provider, and many more. Manage your dependency according to the console panel issue or use the command "flutter pub downgrade". Your problem will be 100% solved.
  5. After doing that if you faced this issue again it means you have not followed the guidelines correctly that I have told you.

Upvotes: 0

Manoj Perumarath
Manoj Perumarath

Reputation: 10194

For null safety to work,

environment:
   sdk: ">=2.12.0 <3.0.0"

should be this version at least. then run flutter clean and flutter pub get it will work.

Upvotes: 23

Ghasem Sadeghi
Ghasem Sadeghi

Reputation: 1854

This problem was occured for me after upgrading the Flutter.
I solved it by cleaning and upgrading the project dependencies again.
Run the below commands in the root directory of your project:

flutter clean
flutter packages pub upgrade
flutter pub run build_runner build

Also as others said, please make sure your sdk version in the pubspec.yaml is compatible with your flutter sdk version.

Upvotes: 53

Smruti Ranjan Rana
Smruti Ranjan Rana

Reputation: 2194

Try

flutter clean

This is worked for me!

Upvotes: 5

Jason Simpson
Jason Simpson

Reputation: 4952

Depending on what you're doing, sometimes it's as simple as changing the environment in your pubspec.yaml file to update the lower end, ie change a lower end like

environment:
sdk: ">=2.0.0 <3.0.0"

to

environment:
sdk: ">=2.6.0 <3.0.0"

or

environment:
sdk: ">=2.7.0 <3.0.0"

This has worked for the various things that cause this error (using spread syntax in latest version of flutter and dart will also cause this), but I don't need the older environments.

Upvotes: 2

Tommie C.
Tommie C.

Reputation: 13181

A simple fix for me was to ensure that the analysis_options.yaml is in the root folder. In my case, it was in the lib folder and none of the experimental features worked until I moved the file to the same folder as pubspec.yaml. Other things that can happen include updates that make the experimental feature no longer required so updating to the latest version on the flutter master channel may be useful in addition to verifying the file location. Haven't been able to get the command line running with this option, however.

Upvotes: 0

Related Questions