Flutter / Firebase 项目 - “未处理的异常:在空值上使用空检查运算符”是否会导致空白屏幕?

Flutter / Firebase project - Is `Unhandled Exception: Null check operator used on a null value` causing a blank screen?

提问人:Alex 提问时间:1/9/2022 最后编辑:Alex 更新时间:7/23/2022 访问量:1509

问:

我正在使用 firebase 进行一个 flutter 项目。我目前正在 MacBook 上开发,使用模拟的 iPhone(以防万一)。我能够运行我的应用程序,但是当这样做时,它只是一个永远不会消失的白色空白屏幕,知道这种情况发生在 Chrome 和模拟 Android 设备上也可能是相关的。在窗口中查看,我可以看到我有一个错误,显示以下内容:run

Launching lib/main.dart on iPhone 13 in debug mode...
Running Xcode build...
Xcode build done.                                           28.4s
Debug service listening on ws://127.0.0.1:61474/fF4XR-ozS5s=/ws
Syncing files to device iPhone 13...
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value
#0      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:121:86)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:146:36)
#2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
#3      MethodChannel.invokeListMethod (package:flutter/src/services/platform_channel.dart:342:41)
#4      MethodChannelFirebase._initializeCore (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:31:37)
#5      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:73:13)
#6      Firebase.initializeApp (package:firebase_core/src/firebase.dart:42:47)
#7      main (package:we_rise/main.dart:8:18)
#8      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:145:25)
#9      _rootRun (dart:async/zon<…>

由于我仍然是一个新手,我无法完全破译出了什么问题,或者我在这里看到了什么。请在下面找到我的文件代码:main.dart

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'database_config.dart';
import 'launch_page.dart';

void main() async {
  await Firebase.initializeApp(
    options: FirebaseOptions(
        apiKey: "foo",//"apiKey",
        appId: "foo",//"appId",
        messagingSenderId: "foo",//"messagingSenderId",
        projectId: "foo"),//"projectId"),
  );
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
    );
    return MaterialApp(
      theme: ThemeData(
        brightness: Brightness.light,
        fontFamily: 'BookmanOldStyle',
      ),
      home: LaunchPage(),
    );
  }
}

我希望这是有用的。我真的不明白为什么它在应用程序加载时向我显示空白屏幕。我也读过这个答案,但我认为我没有在我的代码中使用过空检查运算符。任何帮助、建议和批评都是值得赞赏的。

编辑:添加了 FLUTTER DOCTOR 输出

flutter doctor输出:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.5.3, on macOS 12.0.1 21A559 darwin-arm, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] Connected device (2 available)

• No issues found!

flutter doctor -v输出:

flutter doctor -v
[✓] Flutter (Channel stable, 2.5.3, on macOS 12.0.1 21A559 darwin-arm, locale en-GB)
    • Flutter version 2.5.3 at /Users/me/Documents/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 18116933e7 (3 months ago), 2021-10-15 10:46:35 -0700
    • Engine revision d3ea636dc5
    • Dart version 2.14.4

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/me/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 13.2.1, Build version 13C100
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[✓] Connected device (2 available)
    • iPhone 13 (mobile) • C0D4EC95-07AF-46AB-A35E-F2B9F4437875 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-2 (simulator)
    • Chrome (web)       • chrome                               • web-javascript • Google Chrome 97.0.4692.71

• No issues found!
Android iOS Firebase Flutter null

评论

1赞 Diwyansh 1/12/2022
您的问题是否已通过以下答案得到解决?
0赞 Alex 1/12/2022
@Diwyansh还没有,它现在加载一个空白屏幕,崩溃,并在 MacOS 中打开一个对话框,该对话框给出错误Runner quit unexpectedly
1赞 Alexey Inkin 1/13/2022
请编辑您的问题以包含命令的输出。flutter doctor
0赞 Alex 1/13/2022
@AlexeyInkin全部完成
1赞 Alexey Inkin 1/13/2022
1) 尝试升级到最新的稳定版 Flutter,目前是 2.8.1。2) 确保您使用最新的 Firebase,从 pubspec.yaml 检查每个 firebase 包。3)仔细检查在所有可用的平台上添加后是否有新的异常。如果例外情况发生变化,请更新您的帖子。WidgetsFlutterBinding.ensureInitialized()

答:

6赞 Mehmet Demir 1/11/2022 #1

您应该添加

WidgetsFlutterBinding.ensureInitialized()

添加到 Firebase 初始化前的 main 函数。

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp(
    options: FirebaseOptions(
        apiKey: "foo",//"apiKey",
        appId: "foo",//"appId",
        messagingSenderId: "foo",//"messagingSenderId",
        projectId: "foo"),//"projectId"),
  );
  await DatabaseConfig.initFirebase();
  runApp(MyApp());
}

评论

0赞 Alex 1/12/2022
它现在加载一个空白屏幕,崩溃,并在 MacOS 中打开一个对话框,该对话框给出错误Runner quit unexpectedly
0赞 Burhanuddin Rashid 1/14/2022
@Alex 你能分享MacOS错误的堆栈跟踪吗?
1赞 Pranav 1/13/2022 #2

我认为其中之一应该有效。

  1. 确保你已经正确初始化了 firebase,即采取了正确的步骤将 firebase 与你的项目集成,flutterfire configure

  2. 在 Firebase.initializeApp- 中添加名称

 await Firebase.initializeApp(

    name: "SecondaryApp",//**Add this line**
    options: FirebaseOptions(
        apiKey: "foo",//"apiKey",
        appId: "foo",//"appId",
        messagingSenderId: "foo",//"messagingSenderId",
        projectId: "foo"),
  );

评论

0赞 Alex 1/13/2022
按照以下步骤在运行终端中输出以下错误: ''' Failed to send request: {“jsonrpc”:“2.0”,“id”:“20”,“method”:“getObject”,“params”:{“isolateId”:“isolates/1668629121630887”,“objectId”:“libraries/@560171358”}} '''
1赞 Pranav 1/13/2022
看看重新运行是否有帮助。来源- stackoverflow.com/questions/54079152/...
1赞 Pranav 1/13/2022
@Alex屏幕还是空白的?
1赞 Pranav 1/13/2022
你能告诉我等待什么吗 DatabaseConfig.initFirebase();是用来的?
1赞 Pranav 1/13/2022
删除该行并将名称添加到后,您可以检查此问题是否仅在iOS设备上发生。代码是否对物理 iOS 和 Android 设备给出了相同的问题?Firebase.initializeApp(...)
1赞 TheFunk 1/14/2022 #3

为了更好地衡量,请尝试“颤振升级”,因为 2.5.3 不再是最新的稳定版本,它们现在在 2.8.1 上。然后,请确保您遵循了此处列出的每个平台设置部分下的所有说明(不折不扣)。

如果你在任何时候必须运行“flutter clean”或删除你的构建文件夹,请确保你检查它们是否有上面该链接中指定的所需包含。我在原来的帖子中看到你说 flutterfire 在你的任何平台上都不起作用,这对我来说表明该库可能没有在每个平台的构建文件夹下完全配置。

0赞 Alex 2/9/2022 #4

对于所有遇到此问题的人来说,对我有用的是在 Windows 版本上创建一个新项目(我使用的是 android studio)并在那里重新制作程序,您甚至可以在大多数情况下复制和粘贴。

在我看来,主要问题是我在 MacOS 上开发时没有 iPhone,因此,当它链接到我的开发人员个人资料时,我无法运行它。