Flutter null 安全问题,无法编译

Posted

技术标签:

【中文标题】Flutter null 安全问题,无法编译【英文标题】:Flutter null safety issues, unable to compile 【发布时间】:2021-06-21 12:26:28 【问题描述】:

我在构建 FLUTTER WEB 应用程序时遇到以下错误:

Error: Cannot run with sound null safety, because the following dependencies don't support null safety:

 - package:html2md
 - package:html
 - package:csslib

For solutions, see https://dart.dev/go/unsound-null-safety ../../../flutter/.pub-cache/hosted/pub.dartlang.org/easy_web_view-1.4.0-nullsafety/lib/src/web.dart:61:5: Warning: Operand of null-aware operation '?.' has type 'EasyWebView' which excludes null.
 - 'EasyWebView' is from 'package:easy_web_view/src/web.dart' ('../../../flutter/.pub-cache/hosted/pub.dartlang.org/easy_web_view-1.4.0-nullsafety/lib/src/web.dart').
    widget?.onLoaded();
    ^ ../../../flutter/.pub-cache/hosted/pub.dartlang.org/easy_web_view-1.4.0-nullsafety/lib/src/web.dart:66:15: Warning: Operand of null-aware operation '?.' has type 'EasyWebView' which excludes null.
 - 'EasyWebView' is from 'package:easy_web_view/src/web.dart' ('../../../flutter/.pub-cache/hosted/pub.dartlang.org/easy_web_view-1.4.0-nullsafety/lib/src/web.dart').
          if (widget?.onLoaded != null) 
              ^ ../../../flutter/.pub-cache/hosted/pub.dartlang.org/easy_web_view-1.4.0-nullsafety/lib/src/web.dart:95:14: Warning: Operand of null-aware operation '?.' has type 'EasyWebView' which excludes null.
 - 'EasyWebView' is from 'package:easy_web_view/src/web.dart' ('../../../flutter/.pub-cache/hosted/pub.dartlang.org/easy_web_view-1.4.0-nullsafety/lib/src/web.dart').
      width: widget?.width,
             ^ ../../../flutter/.pub-cache/hosted/pub.dartlang.org/easy_web_view-1.4.0-nullsafety/lib/src/web.dart:96:15: Warning: Operand of null-aware operation '?.' has type 'EasyWebView' which excludes null.
 - 'EasyWebView' is from 'package:easy_web_view/src/web.dart' ('../../../flutter/.pub-cache/hosted/pub.dartlang.org/easy_web_view-1.4.0-nullsafety/lib/src/web.dart').
      height: widget?.height,
              ^ ../../../flutter/.pub-cache/hosted/pub.dartlang.org/easy_web_view-1.4.0-nullsafety/lib/src/web.dart:124:20: Warning: Operand of null-aware operation '?.' has type 'EasyWebView' which excludes null.
 - 'EasyWebView' is from 'package:easy_web_view/src/web.dart' ('../../../flutter/.pub-cache/hosted/pub.dartlang.org/easy_web_view-1.4.0-nullsafety/lib/src/web.dart').
              key: widget?.key,
                   ^ Unhandled exception: Bad state: Unsupported Null Safety mode NonNullableByDefaultCompiledMode.Invalid, in null.
#0      ProgramCompiler.emitModule (package:dev_compiler/src/kernel/compiler.dart:435:9)
#1      javascriptBundler.compile (package:frontend_server/src/javascript_bundle.dart:144:33)
#2      FrontendCompiler.writeJavascriptBundle (package:frontend_server/frontend_server.dart:632:20) <asynchronous suspension>
#3      FrontendCompiler.compile (package:frontend_server/frontend_server.dart:545:9) <asynchronous suspension>
#4      listenAndCompile.<anonymous closure> (package:frontend_server/frontend_server.dart:1105:11) <asynchronous suspension> Finished with error: the Dart compiler exited unexpectedly. Failed to compile application.

以下是我的依赖项:

  cupertino_icons: ^1.0.2
  http: ^0.13.1
  json_annotation: ^4.0.1
  provider: ^5.0.0
  google_fonts: ^2.0.0
  vibration: ^1.7.4-nullsafety.0 #^1.7.4
  firebase_core: ^1.0.2
  cloud_firestore: ^1.0.2
  easy_web_view: ^1.4.0-nullsafety #easy_web_view: ^1.3.0
  intl: ^0.17.0
  uuid: ^3.0.2
  url_launcher: ^6.0.2
  shared_preferences: ^2.0.5

【问题讨论】:

【参考方案1】:

easy_web_view: ^1.4.0-nullsafety 使用版本 0.5.1 中的包 html2md,它不完全支持 null 安全性。

html2md 在支持空值安全的 1.0.0 版本中可用,但使用此新版本的 easy_web_view 没有更新。

您将不得不等待easy_web_view 的维护者创建更新版本,或者您可以帮助并分支并自己做。

【讨论】:

@Damandroid 可能会在github.com/rodydavis/easy_web_view 中创建一个关于它的问题 我会尽快更新!请提交 PR

以上是关于Flutter null 安全问题,无法编译的主要内容,如果未能解决你的问题,请参考以下文章

无法以可靠的 null 安全性运行,因为以下依赖项不支持 null 安全性

Flutter intl 生成文件,null 安全问题

Flutter null 安全条件小部件

即构音视频 Express Flutter SDK 全面支持空安全

如何修复 Flutter build_runner 错误:无法预编译 build_runner:build_runner:

Flutter null 安全性 - 参数类型“颜色?”不能分配给参数类型“颜色”