Flutter 构建 Web 构建没有健全的空安全性并且运行出错

时间:2021-03-29 09:14:33

标签: flutter flutter-web

运行 flutter build web 会出现以下错误:

flutter build web
Changing current working directory to: /home/forest/projects/icell/i_cell_parking_manager

Building without sound null safety
For more information see https://dart.dev/null-safety/unsound-null-safety

Target dart2js failed: Exception: ../../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore_web-1.0.1/lib/src/interop/utils/utils.dart:15:41:
Error: The argument type 'Object? Function(Object)' can't be assigned to the parameter type 'Object? Function(Object?)?' because 'Object?' is nullable and 'Object' isn't.
 - 'Object' is from 'dart:core'.
  return core_interop.dartify(jsObject, (Object object) {
                                        ^
../../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore_web-1.0.1/lib/src/interop/utils/utils.dart:39:41:
Error: The argument type 'dynamic Function(Object)' can't be assigned to the parameter type 'Object? Function(Object?)?' because 'Object?' is nullable and 'Object' isn't.
 - 'Object' is from 'dart:core'.
  return core_interop.jsify(dartObject, (Object object) {
                                        ^
Error: Compilation failed.


Compiling lib/main.dart for the Web...                              8.3s
Exception: Failed to compile application for the Web.

为 android 构建工作没有任何错误 (flutter build apk)。

flutter doctor

Flutter 2.0.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 4d7946a68d (10 days ago) • 2021-03-18 17:24:33 -0700
Engine • revision 3459eb2436
Tools • Dart 2.12.2

Running flutter doctor...
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.3, on Linux, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] IntelliJ IDEA Ultimate Edition (version 2020.2)
[✓] Connected device (2 available)
! Doctor found issues in 1 category.

(没有安装 Androd studio 很奇怪,因为我正在从它运行项目,我使用 AVD 的虚拟设备没有问题......)

我的 index.html 导入了最新的库:

...

  <script src="https://www.gstatic.com/firebasejs/7.8.0/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.20.0/firebase-app.js"></script>

相关pubspec.yaml部分:

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  flutter_spinkit: "^4.1.2"
  cloud_firestore: ^1.0.1
  cloud_functions: "^1.0.2"
  provider: ^5.0.0
  google_sign_in: ^5.0.0
  firebase_core: ^1.0.1
  firebase_messaging: ^9.0.0
  firebase_auth: ^1.0.1

2 个答案:

答案 0 :(得分:2)

要在没有空安全的情况下进行发布构建:

flutter build web --no-sound-null-safety

生成的文件将放置在 build/web 下。

<块引用>

注意--no-sound-null-safety 必须作为标志传递给 web 子命令,而不是传递给 buildflutter .


<块引用>

NOTE2:构建集 <base href="/"> 因此除非您从网络服务器根目录提供您的应用程序,否则您需要将值调整为正确的路径。有一个 github issue 可以让 base href 通过命令行参数进行配置。

答案 1 :(得分:0)

解决方案是运行

flutter pub upgradeflutter pub outdated 然后 flutter pub get 解决了这个问题。

我怀疑某些东西被缓存了,并且并不是所有的新依赖项都在开始时用于 web 构建,但不知道为什么它突然起作用:\