Flutter使用Provider实现退出登录后跳转登录页失败如何解决?
问题根因
你当前的实现仅将Consumer<Profile>包裹了MaterialApp的home属性,仅根路由会响应Profile的状态变化。当用户登录后跳转到其他子页面,路由栈中已经存在多个页面实例,此时修改isAuthenticated只会触发根路由重绘,不会影响已存在的子路由栈,因此页面不会跳转。
推荐解决方案
方案1:全局状态监听自动跳转(推荐,符合状态驱动设计)
你已经定义了全局navigatorKey,可以直接在应用初始化时监听Profile的认证状态变化,一旦检测到未登录就清空全部路由栈跳转到登录页,无需在每个退出按钮处重复写跳转逻辑。
修改_MyAppState的initState方法即可:
@override void initState() { super.initState(); initPlatformState(); // 新增状态监听 WidgetsBinding.instance.addPostFrameCallback((_) { final profile = Provider.of<Profile>(context, listen: false); profile.addListener(() { if (!profile.isAuthenticated) { // 清空全部路由栈,跳转到登录页 navigatorKey.currentState?.pushAndRemoveUntil( MaterialPageRoute(builder: (context) => AuthScreen()), (route) => false, ); } }); }); }
注意:需要确保你的
navigatorKey是全局定义的GlobalKey<NavigatorState>类型,示例定义如下:final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
方案2:退出登录时主动触发跳转
也可以直接在退出登录的点击事件中补充跳转逻辑,适合仅需在少量位置处理退出逻辑的场景:
ListTile( leading: Icon(Icons.logout), title: Text(AppLocalizations.of(context)!.logout), onTap: () async { SharedPreferences preferences = await SharedPreferences.getInstance(); await preferences.clear(); final Profile profile = Provider.of<Profile>(context, listen: false); profile.isAuthenticated = false; // 新增跳转逻辑,清空路由栈跳转到登录页 if (context.mounted) { Navigator.of(context).pushAndRemoveUntil( MaterialPageRoute(builder: (context) => const AuthScreen()), (route) => false, ); } }),
额外注意点
登录成功后跳转主页时也建议使用pushAndRemoveUntil清空登录页的路由栈,避免用户滑动返回时回到登录页。
内容的提问来源于stack exchange,提问作者Giacomo M
相关产品推荐
相关产品推荐

