Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

dart

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?

like image 258
user3612643 Avatar asked Sep 14 '19 09:09

user3612643


People also ask

How do you fix a non-nullable in flutter?

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.

What does non-nullable by default mean in DART?

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.

What is non-nullable in flutter?

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.


3 Answers

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.

like image 126
Ghasem Sadeghi Avatar answered Nov 11 '22 22:11

Ghasem Sadeghi


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.

like image 40
Manoj Perumarath Avatar answered Nov 11 '22 23:11

Manoj Perumarath


Try

flutter clean

This is worked for me!

like image 23
Smruti Ranjan Rana Avatar answered Nov 11 '22 23:11

Smruti Ranjan Rana