Reputation: 5772
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
Reputation: 1
Upvotes: 0
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
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
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
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