gpt4 book ai didi

android - Flutter/Firebase 项目 - `Unhandled Exception: Null check operator used on a null value` 是否导致黑屏?

转载 作者:行者123 更新时间:2023-12-05 00:13:02 27 4
gpt4 key购买 nike

我正在使用 firebase 进行 flutter 项目。我目前正在使用模拟的 iPhone 在 MacBook 上进行开发(以防万一)。我可以运行我的应用程序,但是这样做时它只是一个永远不会消失的白色空白屏幕,知道这也可能发生在 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(),
);
}
}
我希望这很有用。我真的不明白为什么当应用程序加载时它给我一个空白的屏幕。我还阅读了 this answer ,但我认为我的代码中没有使用空检查运算符。感谢任何帮助、建议和批评。
编辑:添加 flutter 医生输出 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!

最佳答案

你应该添加

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());
}

关于android - Flutter/Firebase 项目 - `Unhandled Exception: Null check operator used on a null value` 是否导致黑屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70634840/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com