I want to use the NNBD Dart feature in my Flutter app today, but I could not find out how to use it.
If I try to use int?
et al., I get the following error:
This requires the 'non-nullable' experiment to be enabled.
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.
As of Flutter 2 (and Dart 2.12), null safety is the new default. This means that by specifying the following SDK constraint:
environment:
sdk: '>=2.12.0 <3.0.0'
You automatically opt into null safety.
You need to make sure that your Dart SDK version constraint is 2.9.0
in your pubspec.yaml
file (use flutter channel master
):
environment:
sdk: ">=2.9.0-8.0 <3.0.0"
Now, you can add an analysis_options.yaml
file next to your pubspec.yaml
file:
analyzer:
enable-experiment:
- non-nullable
You should not expect compilation to succeed as of now.
change sdk version on given below
environment:
sdk: ">=2.12.0 <3.0.0"
then, run flutter pub get
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