【问题标题】:Generator cannot target libraries that have not been migrated to null-safety生成器无法定位尚未迁移到空安全的库
【发布时间】:2023-04-05 18:13:01
【问题描述】:

尝试以下命令时会出现此问题:

flutter pub run build_runner build --delete-conflicting-outputs

错误信息:

[SEVERE] json_serializable:json_serializable on lib/models/shipping_address/shipping_address.dart:

Generator cannot target libraries that have not been migrated to null-safety.
package:deals_and_delivery/models/shipping_address/shipping_address.dart:6:7
  ╷
6 │ class ShippingAddress {
  │       ^^^^^^^^^^^^^^^
  ╵
[INFO] Running build completed, took 3.6s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 49ms

[SEVERE] Failed after 3.7s
pub finished with exit code 1

pubspec.yaml:

dependencies:
  json_annotation: ^4.0.0
  flutter:
    sdk: flutter
...
dev_dependencies:
  build_runner: ^1.11.5
  json_serializable: ^4.0.2
  flutter_test:
    sdk: flutter

这些是当前的颤振和飞镖版本:

[√] Flutter (Channel stable, 2.0.0, on Microsoft Windows [Version
> 10.0.19042.844], locale en-US)
>     • Flutter version 2.0.0 at C:\flutter
>     • Framework revision 60bd88df91 (22 hours ago), 2021-03-03 09:13:17 -0800
>     • Engine revision 40441def69
>     • Dart version 2.12.0

我卡在这点上,如何解决这个问题。

【问题讨论】:

    标签: flutter dart dart-null-safety


    【解决方案1】:

    我发现json_serializable >=4.0.0 依赖于json_annotation >=4.0.0 <4.1.0json_annotation: ^4.0.0 包含Null Safetyjson_serializable: ^4.0.2 不包含,因此发生了错误。

    所以我降级了这两个包:

    json_annotation: 3.1.1
    

    json_serializable: 3.5.1
    

    它们又可以正常工作了。

    【讨论】:

    • 我陷入了奇怪的依赖地狱。 depends on both build_runner 1.12.2 and json_serializable 3.5.1, version solving failed.
    • 使用 build_runner: ^1.10.9
    • 使用 json_annotation: ^3.0.1, build_runner: ^1.0.0, json_serializable: ^3.3.0 它对我有用。
    【解决方案2】:

    我认为这完全取决于您是否打算升级整个项目以启用null safety。如果你想使用最新的 json_serializable 包(启用了 null 安全),你需要在你的环境中指定它。

    在您的 pubspec.yaml 中,如果您使用以下内容启用 null 安全性:

    environment:
       sdk: ">=2.12.0 <3.0.0"
    

    ...那么最新的 json_serializable 包应该可以正常工作。

    参考:Behind the scenes: SDK constraints

    如果他们给你这个错误,你的“sdk”环境可能低于 2.12.0。

    但是,如果您对更新 null 安全性不感兴趣,那么您可能需要像您提到的那样将关联的 json_serializable 包降级。

    【讨论】:

      【解决方案3】:

      在您的pubspec.yaml 文件中,较低的SDK 版本应为&gt;=2.12

      environment:
        sdk: '>=2.12.0 <3.0.0'
      

      使用以下版本(都是空安全的)

      json_annotation: ^4.0.1
      json_serializable: ^4.1.2
      build_runner: ^2.0.3
      

      之后,运行:

      flutter pub get
      flutter pub run build_runner build --delete-conflicting-outputs
      

      【讨论】:

        【解决方案4】:

        尝试设置:

        sdk: '>=2.12.0 <3.0.0'
        

        当我使用 json_serializable: ^4.0.1json_anotation: ^4.0.1 时,它对我有用

        【讨论】:

          【解决方案5】:

          我遇到了同样的问题并通过以下步骤解决:

          1. 设置sdk: '&gt;=2.12.0 &lt;3.0.0'

          2. json_serializablejson_annotation 的版本更改为^4.0.0 (不是4.1.0)

          【讨论】:

            【解决方案6】:

            升级您的 pubspec 文件,

              sdk: '>=2.12.0 <3.0.0'
            
              json_annotation: ^4.0.1
              json_serializable: ^4.1.0
              build_runner: ^1.12.2
            

            升级包后运行此命令:

            flutter pub run build_runner build --delete-conflicting-outputs
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-12-27
              • 2021-04-17
              • 2016-04-02
              • 1970-01-01
              • 1970-01-01
              • 2022-01-23
              • 2021-07-14
              • 2019-11-17
              相关资源
              最近更新 更多