【问题标题】:How to properly implement logout functionality in flutter app?如何在颤振应用程序中正确实现注销功能?
【发布时间】:2019-11-20 21:43:55
【问题描述】:

我想在我的 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(),
     )
);

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    不要使用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,
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多