如何解决我正在尝试让用户登录,但是我的代码仅在应用程序热重启后才有效
我正在练习一个登录页面,几乎所有内容都已完成。现在,在后台清除应用程序后,使用户保持登录状态的原因是什么。我曾在Flutter中尝试过sharedPreference,这有效。但是它仅在hotRestart而不在重启时才起作用。有人可以帮我吗?
这是sharedPreference类
import 'package:shared_preferences/shared_preferences.dart';
class HelperFunction {
static String sharedPreferenceUserLoggedInKey = 'userLoggedIn';
static String sharedPreferenceUserLoggedOutKey = 'userLoggedOut';
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);
}
static Future<bool> saveUserLoggedOutSharedPreference(
bool isUserLoggedOut) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setBool(
sharedPreferenceUserLoggedOutKey,isUserLoggedOut);
}
//getting data to sharedPreference
static Future<bool> getUserLoggedInSharedPreference() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.getBool(sharedPreferenceUserLoggedInKey);
}
static Future<bool> getUserLoggedOutSharedPreference() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.getBool(sharedPreferenceUserLoggedOutKey);
}
static Future<bool> getUserSignedUpSharedPreference() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.getBool(sharedPreferenceUserSignedUpKey);
}
}
按下按钮上的登录按钮
FlatButton(
onpressed:(){
HelperFunction.saveUserLoggedInSharedPreference(true);
HelperFunction.saveUserSignedUpSharedPreference(false);
HelperFunction.saveUserLoggedOutSharedPreference(false);
Navigator.pushReplacement(
context,MaterialPageRoute(
builder: (context) => DashBoard(),),);
}
),
FlatButton(
onpressed:(){
HelperFunction.saveUserLoggedInSharedPreference(false);
HelperFunction.saveUserSignedUpSharedPreference(true);
HelperFunction.saveUserLoggedOutSharedPreference(false);
Navigator.pop(context);
}
}
)
FlatButton(
onpressed:(){
HelperFunction.saveUserLoggedOutSharedPreference(true);
HelperFunction.saveUserLoggedInSharedPreference(false);
HelperFunction.saveUserSignedUpSharedPreference(false);
Navigator.pop(context);
}
}
)
这是我的主要功能
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;
bool isUserLoggedOut;
void getLoggedInStatus() async {
await HelperFunction.getUserLoggedInSharedPreference().then((value) {
isUserLoggedIn = value;
print('isUserLoggedIn = $isUserLoggedIn');
});
}
void getSignedUpStatus() async {
await HelperFunction.getUserSignedUpSharedPreference().then((value) {
isUserSignedUp = value;
print('isUserSignedUp = $isUserSignedUp');
});
}
void getLoggedOutStatus() async {
await HelperFunction.getUserLoggedOutSharedPreference().then((value) {
isUserLoggedOut = value;
print('isUserLoggedOut = $isUserLoggedOut');
});
}
@override
void initState() {
getLoggedInStatus();
getSignedUpStatus();
getLoggedOutStatus();
super.initState();
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context,constraints) {
return OrientationBuilder(builder: (context,orientation) {
SizeConfig().init(constraints,orientation);
return MaterialApp(
home: isUserLoggedIn == true
? DashBoard()
: isUserSignedUp == true
? LoginScreen()
: isUserLoggedOut == true ? LoginScreen() : WelcomeScreen(),debugShowCheckedModeBanner: false,);
});
});
}
}
解决方法
搜索了很长时间后,我找到了答案。通过将值放在setState中,我解决了我的问题。
import 'package:firebase_core/firebase_core.dart';
import 'package:flash/helper/helper_function.dart';
import 'package:flash/screens/dash_board_screen.dart';
import 'package:flash/screens/forgot_password_screen.dart';
import 'package:flash/screens/welcome_screen.dart';
import 'package:flash/size_configuration.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flash/screens/login_screen.dart';
import 'package:flash/screens/registration_screen.dart';
import 'package:flash/screens/chat_screen.dart';
import 'package:device_preview/device_preview.dart';
import 'package:flutter/services.dart';
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;
bool isUserLoggedOut;
@override
void initState() {
getLoggedInStatus();
getSignedUpStatus();
getLoggedOutStatus();
super.initState();
}
void getLoggedInStatus() async {
await HelperFunction.getUserLoggedInSharedPreference().then((value) {
setState(() {
isUserLoggedIn = value;
});
print('isUserLoggedIn = $isUserLoggedIn');
});
}
void getSignedUpStatus() async {
await HelperFunction.getUserSignedUpSharedPreference().then((value) {
setState(() {
isUserSignedUp = value;
});
print('isUserSignedUp = $isUserSignedUp');
});
}
void getLoggedOutStatus() async {
await HelperFunction.getUserLoggedOutSharedPreference().then((value) {
setState(() {
isUserLoggedOut = value;
});
print('isUserLoggedOut = $isUserLoggedOut');
});
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context,constraints) {
return OrientationBuilder(builder: (context,orientation) {
SizeConfig().init(constraints,orientation);
return MaterialApp(
home: isUserLoggedIn == true
? DashBoard()
: isUserSignedUp == true || isUserLoggedOut == true
? LoginScreen()
: WelcomeScreen(),debugShowCheckedModeBanner: false,);
});
});
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。