user3612643
user3612643

Reputation: 5772

Non-nullable by default: how to enable the experiment?

I just tried the following in DartPad:

void main() {
  int? x;
}

and get the following error:

Error compiling to JavaScript:
main.dart:2:6:
Error: This requires the 'non-nullable' experiment to be enabled.
  1. How do I enable that experiment? I am using the Flutter SDK.

  2. Does the experiment support null-safety static analysis already?

Upvotes: 10

Views: 10329

Answers (5)

maxpill
maxpill

Reputation: 1331

Manually removing pubspec.lock and running flutter pub get resolved the problem. In my case it was due to regression.

Upvotes: 1

mikyou
mikyou

Reputation: 61

Enable the non-nullable experiment with three steps:

  1. Add dart-sdk version into pubspec.yaml

    environment:
      sdk: '>=2.8.0-dev.0 <2.8.0'
    
  2. Add enable non-nullable experiment into analysis_options.yaml

    analyzer:
      enable-experiment:
        - non-nullable
    
  3. Run your Dart code:

    dart --enable-experiment=non-nullable ./bin/hello_dart.dart
    

Upvotes: 6

AInoob
AInoob

Reputation: 41

For me, removing pubspec.lock and rerun fix the issue. I was importing the project from Windows to Mac.

Upvotes: 3

lrn
lrn

Reputation: 71623

You enable the experiment by passing the flag --enable-experiment=non-nullable to the compiler or analyzer.

It is not a complete feature yet, so there are no promises about what it will do. Feel free to experiment, but don't use the flag for anything serious.

Upvotes: 5

Add the following line to the analysis_options.yaml

analyzer:
  enable-experiment:
    - non-nullable

Upvotes: 18

Related Questions