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

后端 未结 5 1750
礼貌的吻别
礼貌的吻别 2021-01-17 20:53

I just tried the following in DartPad:

void main() {
  int? x;
}

and get the following error:

Error compiling to JavaScript         


        
5条回答
  •  北荒
    北荒 (楼主)
    2021-01-17 21:36

    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
      

提交回复
热议问题