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
?
You can solve this in a few ways. The easiest would be to give an initial value: static List<Category> categories = []; Other ways to handle this without giving an initial value would be to make the variable nullable, or marking it with a late initialization keyword.
The Dart language now supports sound null safety! When you opt into null safety, types in your code are non-nullable by default, meaning that variables can't contain null unless you say they can.
This means that variables are considered non-nullable by default. If you don't give an object null support when you create it, it will never get a null . As a result, you avoid null reference errors in your code.
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.
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.
Try
flutter clean
This is worked for me!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With