Flutter:FCM 未处理的异常:空值检查运算符使用的空值
Flutter: FCM Unhandled Exception: Null check operator used on a null value
E/flutter (26872): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)]
Unhandled Exception: Null check operator used on a null value
E/flutter (26872): #0
MethodChannelFirebaseMessaging.registerBackgroundMessageHandler
(package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:173:53)
E/flutter (26872): #1
FirebaseMessagingPlatform.onBackgroundMessage=
(package:firebase_messaging_platform_interface/src/platform_interface/platform_interface_messaging.dart:108:16)
// Background Messaging Set Up
Future<void> _firebaseMessagingBackgroundHandler(
RemoteMessage message) async {
print('background message');
}
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(....)
我在 Android 系统上遇到此代码的错误。一切正常,除非应用程序终止。
什么适用于 Android:
- 终止时的通知,onBackground 和 onForeground
- 仅当 onForeground 时显示日期
什么在 Android 上不起作用:
- 仅在终止和 onBackground 时数据
什么适用于 iOS:
- 终止时的通知,onBackground 和 onForeground
- 仅当 onForeground 时显示日期
什么在 iOS 上不起作用:
- 仅在终止时数据,
我不知道为什么我在 Android 系统上收到空值错误,我该如何解决这个问题?另外,当应用程序终止时,我在 iOS 上收不到 Data only
推送通知是真的吗?
我和你一样的错误,在同一条线上。我检查了 docs,它说了关于后台消息处理程序的两件事。
- 不能是匿名函数
- 它必须是顶级函数(例如,不是需要初始化的 class 方法)。
在我的例子中,它不是顶级函数,它是在 class 中声明的。当您将处理程序移出任何 class 或函数,使其成为顶级函数并且不需要任何 class 或方法初始化时,错误就会消失。
_firebaseMessagingBackgroundHandler 函数应该在主函数之外。
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
}
Future<void> main() async {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(
...
);
}
就我而言,仅按照文档所述进行操作是不够的。所以我意识到我应该在 main 函数的所有内容之前添加 WidgetsFlutterBinding.ensureInitialized()
,如下所示:
void main() {
WidgetsFlutterBinding.ensureInitialized();
FirebaseMessaging.onBackgroundMessage(_handleMessage);
runApp(const Homino());
}
E/flutter (26872): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value E/flutter (26872): #0
MethodChannelFirebaseMessaging.registerBackgroundMessageHandler (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:173:53) E/flutter (26872): #1
FirebaseMessagingPlatform.onBackgroundMessage= (package:firebase_messaging_platform_interface/src/platform_interface/platform_interface_messaging.dart:108:16)
// Background Messaging Set Up
Future<void> _firebaseMessagingBackgroundHandler(
RemoteMessage message) async {
print('background message');
}
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(....)
我在 Android 系统上遇到此代码的错误。一切正常,除非应用程序终止。
什么适用于 Android:
- 终止时的通知,onBackground 和 onForeground
- 仅当 onForeground 时显示日期
什么在 Android 上不起作用:
- 仅在终止和 onBackground 时数据
什么适用于 iOS:
- 终止时的通知,onBackground 和 onForeground
- 仅当 onForeground 时显示日期
什么在 iOS 上不起作用:
- 仅在终止时数据,
我不知道为什么我在 Android 系统上收到空值错误,我该如何解决这个问题?另外,当应用程序终止时,我在 iOS 上收不到 Data only
推送通知是真的吗?
我和你一样的错误,在同一条线上。我检查了 docs,它说了关于后台消息处理程序的两件事。
- 不能是匿名函数
- 它必须是顶级函数(例如,不是需要初始化的 class 方法)。
在我的例子中,它不是顶级函数,它是在 class 中声明的。当您将处理程序移出任何 class 或函数,使其成为顶级函数并且不需要任何 class 或方法初始化时,错误就会消失。
_firebaseMessagingBackgroundHandler 函数应该在主函数之外。
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
}
Future<void> main() async {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(
...
);
}
就我而言,仅按照文档所述进行操作是不够的。所以我意识到我应该在 main 函数的所有内容之前添加 WidgetsFlutterBinding.ensureInitialized()
,如下所示:
void main() {
WidgetsFlutterBinding.ensureInitialized();
FirebaseMessaging.onBackgroundMessage(_handleMessage);
runApp(const Homino());
}