firebase - 如何在颤振中使用 sharedPreference 以使用 setBool 和 GetBool 保持用户在颤振中登录
问题描述
我正在颤振中练习电子邮件身份验证,几乎一切都结束了。现在,我想使用 sharedPreference 让用户保持登录状态。我尝试了一些方法,但没有得到结果。我正在使用 bool 类型来获取用户是否登录。但我对此很陌生,你能帮我吗?有什么我遗漏的吗?
这是我正在使用的 sharedPreference 静态类
class sharedPreference {
static String sharedPreferenceUserLoggedInKey = 'userLoggedIn';
static String sharedPreferenceUserSignedUpKey = 'userSignedUp';
//saving data to sharedPreference
static Future<bool> saveUserLoggedInSharedPreference(
bool isUserLoggedIn) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setBool(sharedPreferenceUserLoggedInKey, isUserLoggedIn);
}
static Future<bool> saveUserSignedUpSharedPreference(
bool isUserSignUp) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setBool(sharedPreferenceUserSignedUpKey, isUserSignUp);
}
//getting data to sharedPreference
static Future<bool> getUserLoggedInSharedPreference() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.getBool(sharedPreferenceUserLoggedInKey);
}
static Future<bool> getUserSignedUpSharedPreference() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.getBool(sharedPreferenceUserSignedUpKey);
}
}
这是触发 setBool 的 signIn 按钮:SignInButton:
FlatButton(
onPressed: ()
{
HelperFunction.saveUserLoggedInSharedPreference(true);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => DashBoard(email: email),
),
})
主要功能
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light
.copyWith(systemNavigationBarColor: Colors.black));
runApp(
DevicePreview(
enabled: kReleaseMode,
builder: (context) => FlashChat(),
),
);
}
class FlashChat extends StatefulWidget {
@override
_FlashChatState createState() => _FlashChatState();
}
class _FlashChatState extends State<FlashChat> {
bool isUserLoggedIn;
bool isUserSignedUp;
void getLoggedInStatus() async {
await HelperFunction.getUserLoggedInSharedPreference().then((value) {
isUserLoggedIn = value;
});
}
void getSignedUpStatus() async {
await HelperFunction.getUserSignedUpSharedPreference().then((value) {
isUserSignedUp = value;
});
}
@override
void initState() {
getLoggedInStatus();
getSignedUpStatus();
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: isUserLoggedIn == true
? DashBoard.id: WelcomeScreen.id,
routes: {
WelcomeScreen.id: (context) => WelcomeScreen(),
LoginScreen.id: (context) => LoginScreen(),
RegistrationScreen.id: (context) => RegistrationScreen(),
DashBoard.id: (context) => DashBoard(),
},
debugShowCheckedModeBanner: false,
);
});
});
解决方案
当用户获得登录集时
prefs.setBool("isLogin", True);
当用户在注销功能中注销时
pref.clear()
并在启动画面或开始时放置此逻辑
SharedPreferences prefs = await SharedPreferences.getInstance();
var isLogin = prefs.getBool("isLogin");
if (isLogin)
{
//Navigate user to the required screen
}
else{
//navigate user to login screen
}
推荐阅读
- javascript - 为什么离线二维码脚本不适用于表情符号?
- c# - 如何从 IReadOnlyList 中查找项目
? - sql - Oracle SQL PIVOT“列定义不明确”
- javascript - D3根据日期计算x位置
- arrays - Pop 操作的堆栈复杂度
- numpy - 你如何反转numpy数组轴中元素的符号
- node.js - 使用 knex 删除 postgres 中的行时延迟
- linux - 在 Docker 网络中使用主机的默认路由进行传出流量
- c# - ASP.NET Web API REST 服务:HTTP 错误 405
- tensorflow - 将两个列表合并到 PCollection