在指向
FirebaseMessaging.onBackgroundMessage(_handleNotificationBackground);
中的 notification_service.dart
行的标题中出现错误。我认为空检查运算符是 ?
和 !
并且我没有在该行中使用任何一个。为什么我会收到此错误?如果需要的话我可以提供更多信息。
main.dart:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
NotificationService(navigatorKey: navigatorKey);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
...
}
notification_service.dart:
class NotificationService {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
final GlobalKey<NavigatorState> navigatorKey;
NotificationService({required this.navigatorKey}) {
_initialize();
}
Future<void> _initialize() async {
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
// Set up Firebase Cloud Messaging
FirebaseMessaging.onBackgroundMessage(_handleNotificationBackground); //ERROR POINTS HERE
...
}
...
}
从您的代码中,prblm 可以从导航键中产生,您已在主函数中定义了它,我的猜测是您没有将密钥提供给 MaterialApp 小部件。 由于您尚未将其提供给 MaterialApp 小部件,并且仍在尝试访问其为 null 的上下文,导致 Navigator Push() 抛出 null 错误,因为上下文为 null。