Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

dart

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?

like image 597
user3612643 Avatar asked Aug 21 '19 20:08

user3612643


People also ask

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.


Video Answer


2 Answers

Add the following line to the analysis_options.yaml

analyzer:
  enable-experiment:
    - non-nullable
like image 139
Karats Mohanraj Avatar answered Sep 18 '22 15:09

Karats Mohanraj


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

like image 38
maxpill Avatar answered Sep 20 '22 15:09

maxpill