在主体中获取当前上下文

时间:2019-01-17 14:13:31

标签: dart flutter

am使用firebase_messaging进行推送通知..并这样做:

Future main() async {

FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();

_firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) {
    print(message['route']);
   //globals.firebase(context, message['route']);
    print('on message $message');
  },
  onResume: (Map<String, dynamic> message) {
    print('on resume $message');
  },
  onLaunch: (Map<String, dynamic> message) {
    print('on launch $message');
  },
);
_firebaseMessaging.requestNotificationPermissions(
    const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.getToken().then((token){
  print('firebase token is');
  print(token);
});


Routes.configureRoutes(globals.router);
Application.router = globals.router;

// Run app from splash page!
runApp(MyApp());

}

但无法在此处获得上下文以调用此方法:

void firebase(BuildContext context, String route) {
router.navigateTo(context, route,
  transition: TransitionType.inFromRight,
  transitionDuration: const Duration(milliseconds: 500));
}

我也尝试过

  

GlobalKey navigatorKey

解决方案,但是我认为它不起作用,因为我正在使用fluro进行导航和路由...

如何解决此问题并在应用程序主体中获取当前上下文?

1 个答案:

答案 0 :(得分:0)

您可以只使用navigatorKey.currentState.push()