flutter - 如何在flutter应用程序中正确实现注销功能?

标签 flutter dart

我想在我的 Flutter 应用程序中实现注销功能。我的侧边菜单抽屉注销中有一个按钮,当用户按下按钮时,我希望用户导航到登录屏幕并弹出所有其他屏幕。

我试过这样做,我的代码是

SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.remove('userPreference');
await Future.delayed(Duration(seconds: 2));

Navigator.of(context)
    .popUntil(ModalRoute.withName(Navigator.defaultRouteName));

Navigator.of(context).pushReplacement(
     MaterialPageRoute(
        builder: (BuildContext context) => 
                LoginScreen(),
     )
);

最佳答案

而不是使用 PushReplacement , 使用 pushAndRemoveUntil .这会推送您的新路线,并从导航堆栈中删除路线路线,直到传入的函数返回 true .如果要删除所有其他屏幕,请传入一个始终返回 false 的函数:

SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.remove('userPreference');
await Future.delayed(Duration(seconds: 2));

Navigator.of(context).pushAndRemoveUntil(
  // the new route
  MaterialPageRoute(
    builder: (BuildContext context) => LoginScreen(),
  ),

  // this function should return true when we're done removing routes
  // but because we want to remove all other screens, we make it
  // always return false
  (Route route) => false,
);

关于flutter - 如何在flutter应用程序中正确实现注销功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56984672/

相关文章:

flutter - 如何使用 FlutterDriver 关闭对话框

flutter - 初始化 flutter 。这可能需要几分钟的时间

flutter - 在 flutter 中如何根据登录用户的类型导航到特定主页?

android - flutter - 重新加载时奖励视频广告错误 : "ad_not_loaded, show failed for rewarded video, no ad was loaded, null)"

android - 我的 Android Studio 无法识别我的智能手机

flutter - Dart 中 getter 和常规方法的区别

ios - 在 Flutter 中监听方向状态;旋转前后

dart - 在AngularDart中使用dart:html库

ios - 如何让iOS应用程序与蓝牙连接?

flutter - 如何让 Flutter 应用程序响应所有类型的屏幕?