尝试以下命令时出现此问题:
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 和 dart 版本:
[√] 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
我被困在这一点上,如何解决这个问题。
答案 0 :(得分:48)
我发现 json_serializable >=4.0.0
依赖于 json_annotation >=4.0.0 <4.1.0
并且 json_annotation: ^4.0.0
包括 Null Safety
但 json_serializable: ^4.0.2
不,因此发生错误。
所以我降级了两个包:
json_annotation: 3.1.1
和
json_serializable: 3.5.1
它们又可以正常工作了。
答案 1 :(得分:24)
我认为这完全取决于您是否打算升级整个项目以启用 null safety。如果您想使用最新的 json_serializable 包(已启用空安全),您需要在您的环境中指定它。
在您的 pubspec.yaml 中,如果您使用以下内容启用空安全:
environment:
sdk: ">=2.12.0 <3.0.0"
...那么最新的 json_serializable 包应该可以正常工作。
参考:Behind the scenes: SDK constraints
如果他们给你这个错误,你的“sdk”环境很可能低于 2.12.0。
但是,如果您对为空安全进行更新不感兴趣,那么您可能需要按照您提到的那样将关联的 json_serializable 包降级。
答案 2 :(得分:2)
尝试设置:
sdk: '>=2.12.0 <3.0.0'
当我使用 json_serializable: ^4.0.1
和 json_anotation: ^4.0.1
时它对我有用
答案 3 :(得分:0)
我遇到了同样的问题并通过以下步骤解决:
设置sdk: '>=2.12.0 <3.0.0'
将 json_serializable
和 json_annotation
的版本更改为 ^4.0.0
(不是 4.1.0)
答案 4 :(得分:0)
升级您的 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
答案 5 :(得分:0)
在您的 pubspec.yaml
文件中,较低的 SDK 版本应为 >=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