默认情况下不可为空:如何启用实验?

use*_*643 10 dart

我刚刚在 DartPad 中尝试了以下操作:

void main() {
  int? x;
}
Run Code Online (Sandbox Code Playgroud)

并得到以下错误:

Error compiling to JavaScript:
main.dart:2:6:
Error: This requires the 'non-nullable' experiment to be enabled.
Run Code Online (Sandbox Code Playgroud)
  1. 如何启用该实验?我正在使用 Flutter SDK。

  2. 实验是否已经支持零安全静态分析?

小智 18

将以下行添加到 analysis_options.yaml

analyzer:
  enable-experiment:
    - non-nullable
Run Code Online (Sandbox Code Playgroud)


小智 6

通过三个步骤启用不可为空的实验:

  1. 将 dart-sdk 版本添加到 pubspec.yaml 中

    environment:
      sdk: '>=2.8.0-dev.0 <2.8.0'
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在 analysis_options.yaml 中添加启用不可为空的实验

    analyzer:
      enable-experiment:
        - non-nullable
    
    Run Code Online (Sandbox Code Playgroud)
  3. 运行你的 Dart 代码:

    dart --enable-experiment=non-nullable ./bin/hello_dart.dart
    
    Run Code Online (Sandbox Code Playgroud)


lrn*_*lrn 5

您可以通过将标志传递--enable-experiment=non-nullable给编译器或分析器来启用实验。

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.