【问题标题】:I migrated to null safety and i cant run "flutter pub run build_runner build" without an error being thrown我迁移到空安全,我无法运行“flutter pub run build_runner build”而不抛出错误
【发布时间】:2021-05-11 08:10:28
【问题描述】:

我已将// @dart=2.9 添加到我的所有文件中,但build_runner 不会在不抛出此错误的情况下执行此操作:

Warning: Operand of null-aware operation '?.' has type 'SendPort' which excludes null. - 'SendPort' is from 'dart:isolate'.  sendPort?.send(result);  ^Error: Cannot run with sound null safety, because the following dependencies don't support null safety: 
- package:build_runner_core 
- package:json_serializable 
- package:moor_generator 
- package:objectbox_generator 
- package:retrofit_generator 
- package:source_gen 
- package:build_config 
- package:build_runner 
- package:build 
- package:json_annotation 
- package:glob 
- package:pool 
- package:crypto 
- package:logging 
- package:watcher 
- package:build_resolvers 
- package:timing 
- package:graphs 
- package:package_config 
- package:yaml 
- package:analyzer 
- package:sqlparser 
- package:objectbox 
- package:dart_style 
- package:built_collection 
- package:code_builder 
- package:dio 
- package:tuple 
- package:retrofit 
- package:pedantic 
- package:checked_yaml 
- package:pubspec_parse 
- package:build_daemon 
- package:args 
- package:io 
- package:convert 
- package:pub_semver 
- package:ffi 
- package:quiver 
- package:moor 
- package:recase 
- package:_fe_analyzer_shared 
- package:built_value 
- package:http_parser 
- package:http_multi_server 
- package:shelf 
- package:stream_transform 
- package:mime 
- package:shelf_web_socket 
- package:web_socket_channel 
- package:cli_util 
- package:fixnum 
- package:synchronizedFor solutions, see https://dart.dev/go/unsound-null-safety.dart_tool/build/entrypoint/build.dart:44:44: Error: The parameter 'sendPort' can't have a value of 'null' because of its type 'SendPort', but the implicit default value is 'null'. - 'SendPort' is from 'dart:isolate'.Try adding either an explicit non-'null' default value or the 'required' modifier.void main(List<String> args, [_i8.SendPort sendPort]) async { 

【问题讨论】:

  • 您是否将所有软件包的版本号更新为空安全版本?由于并非所有这些都可能支持 null-safety,因此您必须使用混合解决方案,我认为它允许 null-safe/non-null-safe。
  • 也请查看此官方指南:dart.dev/null-safety/unsound-null-safety

标签: flutter dart dart-pub dart-null-safety build-runner


【解决方案1】:

请确保您的所有软件包都更新到各自的空安全版本。对于构建运行器,请使用^1.12.2,然后在当前工作目录中的终端中运行以下命令。

flutter clean
flutter pub cache repair
flutter pub run build_runner clean
flutter pub get

然后运行构建运行器命令。

这对我有用,我已经使用构建运行器成功生成了文件。

【讨论】:

  • 虽然这是一个合理的建议,但它并没有为我解决问题,我确实删除了曾经工作过的 .pub-cache 文件夹,但我认为这相当于颤振 pub 缓存修复
  • 这里有一个未解决的问题github.com/dart-lang/build/issues/3046
  • 也许你是对的,但上面的命令确实对我有用。基本上,cache repair 命令对系统缓存中的所有托管包和 git 包执行全新的重新安装。 dart.dev/tools/pub/cmd/pub-cache
【解决方案2】:

删除你flutter文件夹中的.pub-cache文件夹,然后运行pub upgrade,之后你应该可以运行了:

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

【讨论】:

    【解决方案3】:

    对于那些正在使用完全可靠的 null-safety 的人,即不使用 @dart=2.9 或其他方式来忽略 null-safety 的人。

    在您的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
    

    【讨论】:

      猜你喜欢
      • 2021-08-14
      • 2022-10-08
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      相关资源
      最近更新 更多