我正在開發一個有登錄頁面的應用程式。
當我在模擬器上運行應用程式,然后當我進行更新以激活熱多載(我正在使用 VS Code)時,應用程式會重繪 而不要求我再次登錄。
但是當我在網路上運行應用程式時,每次我必須重新加載應用程式時,我都需要輸入我的登錄資訊。
你知道如何解決這個問題嗎?
謝謝。
uj5u.com熱心網友回復:
我沒有找到這個問題的簡單答案,但我所做的是在我的main和LoginScreen中添加了一些邏輯并使用SharedPreferences。
在我的LoginScreen中,成功驗證身份驗證后,我將登錄資訊設定在SharedPreferences中,并將“isLoggedIn”布林值設定為 true :
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('email', encrypt(data.name, key));
prefs.setString('password', encrypt(data.password, key));
prefs.setBool("isLoggedIn", true);
在我的main的initState函式中,我檢查我是否應該已經登錄,并使用我已經存盤在 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來選擇我的應用程式將根據我的登錄布林值回傳哪個螢屏:
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(),
);
}
In my logout function I set "isLoggedIn" to false :
prefs.setBool("isLoggedIn", false);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/441596.html
