首页 > 解决方案 > 成功迁移到空安全后,Flutter 项目未运行

问题描述

成功迁移到空安全后,我在运行颤振项目时遇到问题。我使用安卓工作室。这是颤振医生的结果:

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.2.1, on Microsoft Windows [Version 10.0.19041.985], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Chrome - develop for the web
[√] Android Studio (version 4.1.0)
[√] Connected device (3 available)

• No issues found!

这是过时的依赖检查的结果:

See https://dart.dev/go/null-safety-migration for a migration guide.

Analyzing project...
[-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\]
All sources appear to be already migrated.  Nothing to do.

但是,我收到由依赖项引起的错误(未直接在我的 pubspec.yaml 中使用,但似乎其中一个包正在使用它,并且此依赖项没有更新空安全性)。因此,当我尝试从 android studio 运行它时,我得到以下输出(即使 dart migrate 甚至应该提到内部包依赖项,因为它可能会阻止运行):

Launching lib\main.dart on Android SDK built for x86 in debug mode...
Running Gradle task 'assembleDebug'...
Error: Cannot run with sound null safety, because the following dependencies
don't support null safety:

 - package:tinycolor

For solutions, see https://dart.dev/go/unsound-null-safety
lib/screens/invitations_screen.dart:115:5: Warning: Operand of null-aware operation '?.' has type 'TabController' which excludes null.
 - 'TabController' is from 'package:flutter/src/material/tab_controller.dart' ('/D:/Flutter/flutterV1.0.0.0/packages/flutter/lib/src/material/tab_controller.dart').
    _c1?.dispose();
    ^
/D:/Flutter/flutterV1.0.0.0/.pub-cache/hosted/pub.dartlang.org/flutter_datetime_picker-1.5.1/lib/flutter_datetime_picker.dart:311:32: Warning: Operand of null-aware operation '??' has type 'Color' which excludes null.
 - 'Color' is from 'dart:ui'.
                  color: theme.backgroundColor ?? Colors.white,
                               ^


FAILURE: Build failed with an exception.

* Where:
Script 'D:\Flutter\flutterV1.0.0.0\packages\flutter_tools\gradle\flutter.gradle' line: 1035

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'D:\Flutter\flutterV1.0.0.0\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 23s
Exception: Gradle task assembleDebug failed with exit code 1

所以我最终尝试在没有 sound-null-safety 的情况下运行,但仍然出现错误(我尝试了 flutter clean --> Invalidate Caches/Restart --> pub get --> flutter run --no-sound-null-safety但仍然是同样的问题):

Using hardware rendering with device Android SDK built for x86. If you notice graphics artifacts, consider enabling software rendering with "--enable-software-rendering".
Launching lib\main.dart on Android SDK built for x86 in debug mode...
Running Gradle task 'assembleDebug'...
Running Gradle task 'assembleDebug'... Done                        22.1s
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Error connecting to the service protocol: failed to connect to http://127.0.0.1:57596/NxvUCmZt3sM=/

注意:在重新启动android studio的最后一次尝试之前,这个最后一个错误也在“错误连接...”语句之前显示了一个异常,我记得这个异常(抱歉没有完整的堆栈跟踪,因为它没有'不再显示):

androidx WindowInsetsCompat ---> Preconditions.checkNonNull 上的 NullPointerException

编辑 在按照@Taur的建议在一个新的项目中完成依赖项之后,事实证明,在没有声音-null-safety的情况下运行时的问题是由flutter_typeahead依赖项引起的,在我运行项目时抛出了前面提到的异常:

E/AndroidRuntime(11173): java.lang.ExceptionInInitializerError
E/AndroidRuntime(11173):    at androidx.core.view.WindowInsetsCompat.<clinit>(WindowInsetsCompat.java:82)
E/AndroidRuntime(11173):    at androidx.core.view.WindowInsetsCompat.toWindowInsetsCompat(WindowInsetsCompat.java:169)
E/AndroidRuntime(11173):    at androidx.core.view.ViewCompat$Api21Impl$1.onApplyWindowInsets(ViewCompat.java:4753)
E/AndroidRuntime(11173):    at android.view.View.dispatchApplyWindowInsets(View.java:9799)
E/AndroidRuntime(11173):    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7076)
E/AndroidRuntime(11173):    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7080)
E/AndroidRuntime(11173):    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7080)
E/AndroidRuntime(11173):    at android.view.ViewRootImpl.dispatchApplyInsets(ViewRootImpl.java:1632)
E/AndroidRuntime(11173):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1734)
E/AndroidRuntime(11173):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1443)
E/AndroidRuntime(11173):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7125)
E/AndroidRuntime(11173):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:935)
E/AndroidRuntime(11173):    at android.view.Choreographer.doCallbacks(Choreographer.java:747)
E/AndroidRuntime(11173):    at android.view.Choreographer.doFrame(Choreographer.java:682)
E/AndroidRuntime(11173):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:921)
E/AndroidRuntime(11173):    at android.os.Handler.handleCallback(Handler.java:873)
E/AndroidRuntime(11173):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(11173):    at android.os.Looper.loop(Looper.java:164)
E/AndroidRuntime(11173):    at android.app.ActivityThread.main(ActivityThread.java:6649)
E/AndroidRuntime(11173):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(11173):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/AndroidRuntime(11173):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
E/AndroidRuntime(11173): Caused by: java.lang.NullPointerException
E/AndroidRuntime(11173):    at androidx.core.util.Preconditions.checkNotNull(Preconditions.java:119)
E/AndroidRuntime(11173):    at androidx.core.view.WindowInsetsCompat.toWindowInsetsCompat(WindowInsetsCompat.java:169)
E/AndroidRuntime(11173):    at androidx.core.view.WindowInsetsCompat.toWindowInsetsCompat(WindowInsetsCompat.java:149)
E/AndroidRuntime(11173):    at androidx.core.view.WindowInsetsCompat$BuilderImpl20.build(WindowInsetsCompat.java:1706)
E/AndroidRuntime(11173):    at androidx.core.view.WindowInsetsCompat$Builder.build(WindowInsetsCompat.java:1588)
E/AndroidRuntime(11173):    at androidx.core.view.WindowInsetsCompat$Impl.<clinit>(WindowInsetsCompat.java:748)
E/AndroidRuntime(11173):    ... 22 more 

标签: flutterdart-null-safety

解决方案


推荐阅读