dart - 在 main 中获取当前上下文

标签 dart flutter

我正在使用 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用于导航和路由 ...

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

最佳答案

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

关于dart - 在 main 中获取当前上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54237805/

相关文章:

list - 如何在 typeahead flutter 中进行 suggestCallback?

flutter - 如何设置 Flutter 自定义通知图标?,因为 fcm 的 flutter 中没有默认通知图标

flutter - ??操作符做什么?

flutter - 在flutter中使用Provider在路由之间传递数据的最佳方法是什么?

android - flutter : How to control conflicting gestures?

flutter - 如何在 flutter 中打开侧面菜单栏?

javascript - 如何从 map 中的小部件搜索和访问数据?

dart - 如何在 Flutter 中将 FAB 转换为操作菜单

dart - 从 dart 语言列表中过滤空值的最佳方法是什么

dart - 使用 VSCode 创建和运行 Dart 控制台应用程序?