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

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?

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!

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.

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.
Dart Reference
Example of Analysis Options File

Related

Dart get won't run

I am facing a problem that really isn't making any sense, a problem that started when I wanted to use the "http" and "provide" dependencies by entering them into the pubspec.yaml file.
pub get won't run due to the dart version being lower than 2.16.1 even though when I run dart --version it informs me that I do have the latest dart version (2.16.1).
I tried finding answers on the internet including on the https://dart.dev/ site. I really don't know what else to do at this point, as this is for my assignment due on the 25th of March 2022. please help.
In your pubspec.yaml, just above where your screenshot cuts off, there is a line that looks like this.
environment:
sdk: ">=2.14.0 <3.0.0"
Update this to the following:
environment:
sdk: ">=2.16.1 <3.0.0"
You updated Dart on your local machine, but you never updated your project to reflect that it should use this version (or a higher version).

Undefined name 'AppLocalizations'. (Documentation) after creating a new project

I created a new Flutter project and did a
flutter pub get
There are no errors from the framework except the one from AppLocalizations class.
Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/app_localizations.dart'.
Another error (obviously because of the same package)
NOTE:
I have already tried:
Restarting IDE + Invalidate Caches and Restart
flutter pub add flutter_gen
Restarting Dart Analysis Server
Updating packages
I'm using: Flutter 2.6.0-11.0.pre • channel dev
You can expect all sorts of surprises when switching between channels (and specifically when using Dev or Master channel).
If switching to stable or beta channel (flutter channel stable and flutter upgrade) due to some reasons is not an option I'd suggest upgrading flutter (newer version might have a fix) cleaning the proj OR recreatign platform projects:
flutter upgrade
flutter clean
flutter create .
Besides, I'd stick to beta channel rather than dev/master - it's typically better tested and has fewer issues.
Note that in 2022 dev channel is no longer available leaving master as the most recent and least tested branch.
flutter_gen/gen_l10n dir is auto generated by flutter and located in .dart_tool. (It's not related to the package named flutter_gen in pub.dev).
Try this,
flutter clean
then,
flutter pub get
If the flutter_gen/gen_l10n dir is not available yet follow the below steps.
Step 0: Make sure you have these in pubspec.yaml,
flutter:
generate: true
and
dependencies:
flutter_localizations:
sdk: flutter
Step 1: Comment everything related to AppLocalization if you can't run the app with them. (Imports & Usage)
Step 2: Now run the app.
Now the flutter_gen/gen_l10n folder will be generated.
Finally: Un-comment everything related to AppLocalization.
Every time you make changes to .arb files you will need to flutter get / run / hot reload / hot restart the app to update generated localization files.
Change these lines in pubspec.yaml file
From
flutter:
uses-material-design: true
To
flutter:
uses-material-design: true
# Enable generation of localized Strings from arb files.
generate: true
Then run:
flutter clean
flutter pub get
flutter run

Flutter: build runner throws a precompile error

I am using analyser 1.7.1. The latest build_runner build command generates the following error.
flutter packages pub run build_runner build Failed to precompile
build_runner:build_runner:
../../../sdk/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.7.1/lib/src/error/best_practices_verifier.dart:1998:14:
Error: A non-null value must be returned since the return type
'String' doesn't allow null. String get displayString {
The usual flutter clean and pub cache repair commands don't seem to be fixing the problem, and displayString doesn't appear anywhere in my codebase.
There's an issue open 9 days ago. Here's the key part:
Right now, the current state of affairs is that:
package:analyzer 1.7.0 requires package:meta ^1.4.0
package:analyzer 1.7.1 has the same contents as 1.7.0, but requires package:meta ^1.3.0
Flutter stable pins package:meta to 1.3.0
I'm uncertain how we're running into the exceptions above - the two most recent versions of analyzer are pretty explicit about which version of meta they need.
#edlman do you have any dependency_overrides in your pubspec?
you're right, I'm using 3rd party pkgs which depend on meta 1.4.0 so I put it to dependency_overrides to solve the collision. It didn't come to my mind it cause such a problem.
I've changed the override to 1.3.0, it works fine, no problem yet
So I'd suggest checking whether or not there's a dependency_overrides in your pubspec, too.
It's a problem with analyser 1.7.1.
Add
dependency_overrides:
analyzer: 1.7.0
to pubspec.yaml.
There are more details available in raina77ow's answer.

Migrating to Flutter 2 fail

I have a project that is developed with Flutter v1.22.6 and was working great, after updating the flutter to v2.0.0 and reading the documentation, I've found out I can use dart migrate --apply-changes to fix issues that are related to flutter upgrade, By running the command I get dozens of errors and the error ends with:
The migration tool didn't start, due to analysis errors.
The following steps might fix your problem:
1. Set the lower SDK constraint (in pubspec.yaml) to a version before 2.12.
2. Run `dart pub get`.
3. Try running `dart migrate` again.
What is the simplest way to migrate the current project to the v2?
The problem is that you have already migrated to a newer version of dart.
You can't migrate your projects from the new version, there is literally nothing to migrate to.
If you want to migrate your older projects (which are without null safety) to a new version, your dart version must be older than 2.12.0 version.
In this case, consider changing the version in your pubspec.yaml file.
environment:
sdk: '>=2.11.0 <3.0.0'
Then run the command in your terminal
> dart migrate --apply-changes
make sure you have following step by step migrate dart 2
and base on your error notification try to set in pubspec.yaml
environment:
# Works in Dart 2 only.
sdk: '>=2.0.0 <3.0.0'

Unable to use spread collections

On using “...” error is shown that this requires the - -spread-collections experiment to be enabled.
Try enabling this experiment by adding it to the command line when compiling and running. error shown
edit: i upgraded with the latest version of flutter but this error is shown
Even I tried adding enable-experiment: -spread-collections in pubspec.yaml file but didn't work
Set the environment in your pubspec.yaml with
environment:
sdk: ">=2.6.0 <3.0.0"