flutter 统一处理token 失效返回登录页面

监听到code ==403 使用eventBus  统一触发。

遇到的问题是

Navigator.of(context).pushAndRemoveUntil(
                new MaterialPageRoute(builder: (context) => new loginPage()
                ), (route) => route == null);
这个方法只在当前页面生效,我是放着首页那块 了首页eventbus 触发,但是页面不跳转

解决办法是换一种不需要context 也能够进行页面跳转的方法。
利用GlobalKey
1.创建全局方法。
class Router {
  static GlobalKey<NavigatorState> navigatorKey = GlobalKey();
}
 
2.在main.dart中的MaterialApp下设置navigatorKey
MaterialApp(
          title: 'app_name',
          theme: kLightTheme,
          navigatorKey: Router.navigatorKey, //设置在这里
      routes: {
        '/LoginPage': (ctx) => loginPage(),'/MainPage': (ctx) => showPlatfrom(),
      },
3.跳转到登录页面
Router.navigatorKey.currentState.pushNamedAndRemoveUntil("/LoginPage", ModalRoute.withName("/"))
 
原文地址:https://www.cnblogs.com/wupeng88/p/12611525.html