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

For*_*stG 2 flutter flutter-web

运行flutter build web出现如下错误:

\n
flutter build web\nChanging current working directory to: /home/forest/projects/icell/i_cell_parking_manager\n\nBuilding without sound null safety\nFor more information see https://dart.dev/null-safety/unsound-null-safety\n\nTarget 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:\nError: 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.\n - 'Object' is from 'dart:core'.\n  return core_interop.dartify(jsObject, (Object object) {\n                                        ^\n../../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore_web-1.0.1/lib/src/interop/utils/utils.dart:39:41:\nError: 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.\n - 'Object' is from 'dart:core'.\n  return core_interop.jsify(dartObject, (Object object) {\n                                        ^\nError: Compilation failed.\n\n\nCompiling lib/main.dart for the Web...                              8.3s\nException: Failed to compile application for the Web.\n
Run Code Online (Sandbox Code Playgroud)\n

为 Android 构建工作时不会出现任何错误 ( flutter build apk)。

\n

flutter doctor

\n
Flutter 2.0.3 \xe2\x80\xa2 channel stable \xe2\x80\xa2 https://github.com/flutter/flutter.git\nFramework \xe2\x80\xa2 revision 4d7946a68d (10 days ago) \xe2\x80\xa2 2021-03-18 17:24:33 -0700\nEngine \xe2\x80\xa2 revision 3459eb2436\nTools \xe2\x80\xa2 Dart 2.12.2\n\nRunning flutter doctor...\nDoctor summary (to see all details, run flutter doctor -v):\n[\xe2\x9c\x93] Flutter (Channel stable, 2.0.3, on Linux, locale en_US.UTF-8)\n[\xe2\x9c\x93] Android toolchain - develop for Android devices (Android SDK version 30.0.2)\n[\xe2\x9c\x93] Chrome - develop for the web\n[!] Android Studio (not installed)\n[\xe2\x9c\x93] IntelliJ IDEA Ultimate Edition (version 2020.2)\n[\xe2\x9c\x93] Connected device (2 available)\n! Doctor found issues in 1 category.\n\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

index.html导入了最新的库:

\n
...\n\n  <script src="https://www.gstatic.com/firebasejs/7.8.0/firebase-auth.js"></script>\n  <script src="https://www.gstatic.com/firebasejs/7.20.0/firebase-app.js"></script>\n\n
Run Code Online (Sandbox Code Playgroud)\n

相关pubspec.yaml部分:

\n
dependencies:\n  flutter:\n    sdk: flutter\n  flutter_localizations:\n    sdk: flutter\n  flutter_spinkit: "^4.1.2"\n  cloud_firestore: ^1.0.1\n  cloud_functions: "^1.0.2"\n  provider: ^5.0.0\n  google_sign_in: ^5.0.0\n  firebase_core: ^1.0.1\n  firebase_messaging: ^9.0.0\n  firebase_auth: ^1.0.1\n
Run Code Online (Sandbox Code Playgroud)\n

ccp*_*zza 6

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

flutter build web --no-sound-null-safety
Run Code Online (Sandbox Code Playgroud)

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

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


注意2:构建集<base href="/">因此除非您从网络服务器根目录提供应用程序,否则您需要将值调整为正确的路径。有一个github 问题base href可以通过命令行参数进行配置。当前文档建议手动设置基本 href 。更新:相应的拉取请求现已合并到主分支。