在 flutter web 上更新代码时保持登录状态

Staying logged in when updating code on flutter web

我正在 flutter 上开发一个有登录页面的应用程序。

当我 运行 模拟器上的应用程序,然后当我进行激活热重载的更新时(我使用的是 VS 代码),应用程序刷新而不要求我再次登录。

但是当我 运行 网络上的应用程序时,每次我必须重新加载应用程序时,我需要输入我的登录信息。

你知道如何解决这个问题吗?

谢谢。

我没有找到这个问题的简单答案,但我所做的是在 mainLoginScreen[=32 中添加了一些逻辑=] 并使用 SharedPreferences.

在我的 LoginScreen 中,在成功验证身份验证后,我在 SharedPreferences 中设置我的登录信息,并将“isLoggedIn”布尔值设置为真:

SharedPreferences prefs = await SharedPreferences.getInstance();
        prefs.setString('email', encrypt(data.name, key));
        prefs.setString('password', encrypt(data.password, key));
        prefs.setBool("isLoggedIn", true);

在我的 maininitState 函数中,我检查我是否应该已经登录并调用我的身份验证函数使用我已经存储在 SharedPreferences 中的登录信息:

if (prefs.getBool("isLoggedIn") == true) {
      await authentification(decrypt(prefs.getString('email').toString(), key), decrypt(prefs.getString('password').toString(), key));
loggedIn = prefs.getBool("isLoggedIn");

我定义了一个 displayFunction 来根据我登录的布尔值来选择我的应用 return 的屏幕 :

Widget displayPage() {
    Widget widget = CircularProgressIndicator();
    if (isDataLoaded == true) {
      if (loggedIn == true) {
        widget = HomePage();
      } else {
        widget = LoginScreen();
      }
    }
    return widget;
  }

Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        textTheme: Theme.of(context).textTheme.apply(
              bodyColor: LightColors.kDarkBlue,
              displayColor: LightColors.kDarkBlue,
              fontFamily: 'Poppins'
            ),
      ),
      home: displayPage(),
    ); 
  }

在我的注销功能中,我将“isLoggedIn”设置为 false :

prefs.setBool("isLoggedIn", false);