Last Updated on 2021-10-23 by Clay
Today I used the customize icon to create the button when I developing a mobile application with Flutter, I got an error message as follow:
Error: This requires the null safety language feature, which is experimental. You can enable the experiment using the '--enable-experiment=non-nullable' command line option.
Simply put, the used icon file that I imported used the experimental grammar on current SDK version.
The IDE detect the grammar and recommend me to update the SDK version to 2.10.0.
Solution
We have to do the following two steps:
- Check the Flutter version (If the version is too low, update SDK version not work either)
- Change the SDK version in pubspec.yaml file
(Optional) Update Flutter version
You can use the following command to update Flutter version:
flutter upgrade
Change SDK version
Add the setting in the pubspec.yaml file:
environment:
sdk: ">=2.12.0 <3.0.0"
The use the following command:
flutter pub get
Maybe you need to restart IDE to display normally.
At this time, you can execute the program and do not get any error message.
References
- https://stackoverflow.com/questions/64621051/how-to-enable-null-safety-in-flutter
- https://stackoverflow.com/questions/57934341/error-this-requires-the-non-nullable-experiment-to-be-enabled
Read More
- [Solved][Flutter] Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 21 declared in library [:camera]
- [Solved][Flutter] This requires the ‘non-nullable’ language feature to be enabled. Try updating your pubspec.yaml to set the minimum SDK constraint to 2.12.0 or higher, and running ‘pub get’.