在GetX状态管理的帮助下,我已将Firebase云消息集成到我的Flutter移动应用程序中。此外,我使用Laravel和Firebase Admin SDK向我的应用程序发送通知。
前台状态和后台状态(已暂停(的通知都正常工作。当我点击应用程序处于终止状态(应用程序关闭(的通知时,问题就开始了。我必须在防溅屏和主页之间设置一个延迟吗?
我试过这样做,但错误仍然发生
//open notif content from terminated state of the app
FirebaseMessaging.instance.getInitialMessage().then((message) {
if (message != null) {
print('TERMINATED');
if (FirebaseAuth.instance.currentUser != null) {
final redirectRoute = message.data['route'];
if (redirectRoute != null) {
//adding delay here since my splashscreen have the same delay
Future.delayed(const Duration(seconds: 6));
bottomnavbarController.updateIndex(int.parse(redirectRoute));
}
} else {
Get.offAllNamed(Routes.LOGIN);
}
}
});
我收到这个错误
Unhandled Exception:
You are trying to use contextless
navigation without a GetMaterialApp or Get.key
这是我的主要艺术
//open notif content from terminated state of the app
FirebaseMessaging.instance.getInitialMessage().then((message) {
if (message != null) {
if (FirebaseAuth.instance.currentUser == null) {
Get.offAllNamed(Routes.LOGIN);
} else {
final redirectRoute = message.data['route'];
if (redirectRoute != null) {
print('TERMINATED');
bottomnavbarController.updateIndex(int.parse(redirectRoute));
}
}
}
});
@override
Widget build(BuildContext context) {
final authController = Get.put(AuthController(), permanent: true);
return FutureBuilder(
future: Future.delayed(const Duration(seconds: 6)),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Obx(
() => GetMaterialApp(
debugShowCheckedModeBanner: false,
title: "Application",
theme: ThemeData(
appBarTheme: const AppBarTheme(
elevation: 0.2,
foregroundColor: Colors.black,
backgroundColor: Colors.white),
),
initialRoute: authController.isSkipIntro.isTrue
? authController.isAuthAndVerified.isTrue
? Routes.PAGEWRAPPER
: Routes.LOGIN
: Routes.INTRODUCTION,
getPages: AppPages.routes,
),
);
}
return FutureBuilder(
future: authController.firstInitialize(),
builder: (context, snapshot) {
return const SplashScreen();
},
);
},
);
}
这是我的底部导航控制器
class BottomnavbarController extends GetxController {
final bottomNavIndex = 0.obs;
void updateIndex(int index){
bottomNavIndex.value = index;
Get.offAllNamed(Routes.PAGEWRAPPER);
}
}
这是日志
Launching libmain.dart on RNE L22 in release mode...
Running Gradle task 'assembleRelease'... 76.4s
√ Built buildappoutputsflutter-apkapp-release.apk (10.0MB).
Installing buildappoutputsflutter-apkapp.apk... 3.4s
Flutter run key commands.
h List all available interactive commands.
c Clear the screen
q Quit (terminate the application on the device).
W/FlutterJNI(10061): FlutterJNI.loadLibrary called more than once
W/FlutterJNI(10061): FlutterJNI.prefetchDefaultFontManager called more than once
W/FlutterJNI(10061): FlutterJNI.init called more than once
W/FlutterJNI(10239): FlutterJNI.loadLibrary called more than once
W/FlutterJNI(10239): FlutterJNI.init called more than once
I/flutter (10239): Handling a background message: 0:1646807530074172%f77b2610f77b2610
I/flutter (10239): TERMINATED
E/flutter (10239): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: You are trying to use contextless navigation without
E/flutter (10239): a GetMaterialApp or Get.key.
E/flutter (10239): If you are testing your app, you can use:
E/flutter (10239): [Get.testMode = true], or if you are running your app on
E/flutter (10239): a physical device or emulator, you must exchange your [MaterialApp]
E/flutter (10239): for a [GetMaterialApp].
E/flutter (10239):
E/flutter (10239): #0 GetNavigation.global (package:get/get_navigation/src/extension_navigation.dart:1094)
E/flutter (10239): #1 GetNavigation.offAllNamed (package:get/get_navigation/src/extension_navigation.dart:786)
E/flutter (10239): #2 BottomnavbarController.updateIndex (package:prototypeapp/app/modules/bottomnavbar/controllers/bottomnavbar_controller.dart:9)
E/flutter (10239): #3 _MyAppState.initState.<anonymous closure> (package:prototypeapp/main.dart:64)
E/flutter (10239): #4 _rootRunUnary (dart:async/zone.dart:1434)
E/flutter (10239): <asynchronous suspension>
E/flutter (10239):
I/flutter (10239): Null check operator used on a null value
I/flutter (10239): #0 PageRedirect.page (package:get/get_navigation/src/routes/route_middleware.dart:199)
I/flutter (10239): #1 GetMaterialApp.initialRoutesGenerate (package:get/get_navigation/src/root/get_material_app.dart:348)
I/flutter (10239): #2 _WidgetsAppState.build.<anonymous closure> (package:flutter/src/widgets/app.dart:1556)
I/flutter (10239): #3 NavigatorState.restoreState (package:flutter/src/widgets/navigator.dart:3290)
I/flutter (10239): #4 RestorationMixin._doRestore (package:flutter/src/widgets/restoration.dart:887)
I/flutter (10239): #5 RestorationMixin.didChangeDependencies (package:flutter/src/widgets/restoration.dart:873)
I/flutter (10239): #6 NavigatorState.didChangeDependencies (package:flutter/src/widgets/navigator.dart:3336)
I/flutter (10239): #7 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4914)
I/flutter (10239): #8 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #9 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #10 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #11 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #12 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #13 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #14 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #15 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #16 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #17 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #18 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #19 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #20 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #21 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #22 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #23 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #24 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #25 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #26 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #27 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #28 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #29 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #30 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #31 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #32 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #35 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #36 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #37 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #38 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #39 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #40 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #41 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #42 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #43 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #44 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #45 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #46 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #47 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #48 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #49 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #50 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #51 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #52 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #53 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #54 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #55 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #56 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #57 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #58 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #59 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #60 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928)
I/flutter (10239): #61 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #62 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #63 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4919)
I/flutter (10239): #64 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #65 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #66 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #67 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #68 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #69 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #70 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #71 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #72 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #73 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #74 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928)
I/flutter (10239): #75 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #76 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #77 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4919)
I/flutter (10239): #78 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #79 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #80 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #81 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #82 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #83 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #84 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #85 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #86 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #87 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #88 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #89 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #90 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #91 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #92 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #93 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
I/flutter (10239): #94 Element.rebuild (package:flutter/src/widgets/framework.dart:4477)
I/flutter (10239): #95 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735)
I/flutter (10239): #96 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729)
I/flutter (10239): #97 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790)
I/flutter (10239): #98 Element.updateChild (package:flutter/src/widgets/framework.dart:3540)
I/flutter (10239): #99 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780)
我现在已经用另一种解决方案解决了这个问题。这是主要的艺术
@override
void initState() {
super.initState();
//open notif content from terminated state of the app
FirebaseMessaging.instance.getInitialMessage().then((message) {
if (message != null) {
print('TERMINATED');
final redirectRoute = message.data['route'];
print('redirectRoute $redirectRoute');
bottomnavbarController.updateIndex(int.parse(redirectRoute));
//remove redirect route here, so the unknownRoute will trigger the default route
}
});
//only works if app in foreground
FirebaseMessaging.onMessage.listen((message) {
LocalNotificationService.display(message);
});
//onclick notif system tray only works if app in background but not termi
FirebaseMessaging.onMessageOpenedApp.listen((message) {
final redirectRoute = message.data['route'];
if (redirectRoute != null) {
print('BACKGROUND');
print('redirectRoute $redirectRoute');
bottomnavbarController.updateIndex(int.parse(redirectRoute));
Get.offAllNamed(Routes.PAGEWRAPPER);
}
});
}
@override
Widget build(BuildContext context) {
final authController = Get.put(AuthController(), permanent: true);
return FutureBuilder(
future: Future.delayed(const Duration(seconds: 6)),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Obx(
() => GetMaterialApp(
debugShowCheckedModeBanner: false,
title: "Application",
theme: ThemeData(
appBarTheme: const AppBarTheme(
elevation: 0.2,
foregroundColor: Colors.black,
backgroundColor: Colors.white),
),
initialRoute: authController.isSkipIntro.isTrue
? authController.isAuthAndVerified.isTrue
? Routes.PAGEWRAPPER
: Routes.LOGIN
: Routes.INTRODUCTION,
getPages: AppPages.routes,
unknownRoute: authController.isAuthAndVerified.isTrue ? AppPages.routes[11] : AppPages.routes[1],
),
);
}
return FutureBuilder(
future: authController.firstInitialize(),
builder: (context, snapshot) {
return const SplashScreen();
},
);
},
);
}
如果路由为null,我在GetMaterialApp
上添加了这个unknownRoute:
,因为getInitialMessage()
中的路由返回null,所以它将一个数组传递到AppPages
类内登录页面的页面包装器索引。
我已经使用Get.key进行了定义,但仍然返回null。也许app_routes.dart
在GetMaterialApp之前还没有创建。也许这就是它返回null的原因。
我仍然不知道为什么会发生这种事。也许这是GetX插件的一个错误。不管怎样,这仍然适用于我,因为我已经更新了底部导航索引。