【问题标题】:Error: This requires the 'non-nullable' experiment to be enabled错误:这需要启用“不可为空”实验
【发布时间】:2019-09-14 09:51:28
【问题描述】:

我正在玩弄不可为空的类型并将其添加到我的analysis_options.yaml

analyzer:
  enable-experiment:
    - non-nullable

我有一个使用可空性扩展的代码生成器。 Visual Code 对我的代码很好。

现在,我尝试运行:

flutter packages pub run build_runner watch 

我收到此错误消息:

[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.

如何将--enable-experiment:non-nullable 传递给flutter packages pub run

如果我运行,也会发生同样的情况:

flutter build ios

我收到错误消息:

  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.

所以,同样的问题:我怎样才能将--enable-experiment:non-nullable 传递给flutter build

【问题讨论】:

标签: flutter dart


【解决方案1】:

我在升级 Flutter 后出现了这个问题。
我通过再次清理和升级项目依赖项解决了这个问题。
在项目的根目录中运行以下命令:

flutter clean
flutter packages pub upgrade
flutter pub run build_runner build

也如其他人所说,请确保您在pubspec.yaml 中的sdk 版本与您的flutter sdk 版本兼容。

【讨论】:

  • 另外,对于该语法,sdk 版本至少应为 sdk: ">=2.12.0
【解决方案2】:

为了安全工作,

environment:
   sdk: ">=2.12.0 <3.0.0"

至少应该是这个版本。 然后运行flutter cleanflutter pub get 它会起作用。

【讨论】:

    【解决方案3】:

    试试

    flutter clean
    

    这对我有用!

    【讨论】:

      【解决方案4】:

      取决于你在做什么,有时就像在你的 pubspec.yaml 文件中改变环境来更新低端一样简单,即改变一个像这样的低端

      environment:
      sdk: ">=2.0.0 <3.0.0"
      

      environment:
      sdk: ">=2.6.0 <3.0.0"
      

      environment:
      sdk: ">=2.7.0 <3.0.0"
      

      这适用于导致此错误的各种情况(在最新版本的颤振和飞镖中使用扩展语法也会导致此错误),但我不需要旧环境。

      【讨论】:

        【解决方案5】:

        对我来说一个简单的解决方法是确保 analysis_options.yaml 在 根文件夹。在我的情况下,它在 lib 文件夹中,并且没有 实验功能一直有效,直到我将文件移动到同一个文件夹 作为 pubspec.yaml。其他可能发生的事情包括更新 使实验功能不再需要,因此更新到 此外,flutter master 频道上的最新版本可能会有用 验证文件位置。无法获取命令行 但是,使用此选项运行。

        【讨论】:

          猜你喜欢
          • 2020-11-18
          • 2021-09-11
          • 2020-02-11
          • 1970-01-01
          • 1970-01-01
          • 2013-10-20
          • 2023-03-23
          • 2020-12-07
          • 1970-01-01
          相关资源
          最近更新 更多