注销后用户仍然登录
user still logged in after logging out
我正在使用 flutter 制作应用程序并且我使用了 shared_preferences 包,并且在授权阶段我面临一个问题,即当我构建应用程序时用户登录以及当我注销并重新启动应用程序时杀了它之后,它仍然在主页上,
这是我的代码
main.dart
bool checkingKey;
Future<bool> checkKey() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool checkingKey=prefs.containsKey("jwt");
print("$checkingKey");
return checkingKey;
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
Paint.enableDithering = true;
await checkKey().then((value){
checkingKey=value;
});
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// bool check=checkKey().then((bool value) => true);
print("hello=$checkingKey");
return MaterialApp(
home: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
),
child: Scaffold(
resizeToAvoidBottomInset: false,
body: Container(
color: Color(0xffccffcc),
child:checkingKey==false?LoginPage():mainPage()
),
),
),
routes: <String,WidgetBuilder>{
'/home':(BuildContext context)=>mainPage(),
'/login':(BuildContext context)=>LoginPage(),
}
);
}
}
login_signup_Auth.dart
Future<void> attemptLogIn(String username, String password,BuildContext context) async {
///?final storage =parent_inherit.of(context);
///?var verify=storage.verify;
SharedPreferences prefs = await SharedPreferences.getInstance();
print("$username $password");
final http.Response res = await http.post(
"https://green-earth.herokuapp.com/signin",
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
// 'authorization':'Bearer '+
},
body: jsonEncode(<String, String>{
"email": username,
"password": password
}),
);
if(res.statusCode == 200) {
prefs.setString('jwt',res.body);
var value=prefs.getString('jwt');
print("storage= ${value.isEmpty}");
Navigator.of(context).pushNamed('/home');
}
else{
return _showMyDialoglogin(context,res.statusCode);
}
}
void logoutOutOfApp(BuildContext context) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.clear();
Navigator.of(context).pushNamedAndRemoveUntil('/login', (Route<dynamic> route) => false);
}
在不做任何更改的情况下第二次构建时,返回了检查键变量'true',我不知道,这怎么可能!!!!!!
我没有明白我做错了什么,如果您看到任何其他可以提高程序效率的问题或任何其他应该使用的代码。请告诉
非常感谢!!
你为什么要把事情复杂化?
您的 main.dart
可以简单地看起来像这样
bool checkingKey;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
Paint.enableDithering = true;
var prefs = await SharedPreferences.getInstance();
checkingKey = prefs.containsKey("jwt");
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("hello=$checkingKey");
return MaterialApp(
home: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
),
child: Scaffold(
resizeToAvoidBottomInset: false,
body: Container(
color: Color(0xffccffcc),
child: !checkingKey ? LoginPage() : mainPage(),
),
),
),
routes: <String,WidgetBuilder>{
'/home':(BuildContext context) => mainPage(),
'/login':(BuildContext context) => LoginPage(),
},
);
}
}
我正在使用 flutter 制作应用程序并且我使用了 shared_preferences 包,并且在授权阶段我面临一个问题,即当我构建应用程序时用户登录以及当我注销并重新启动应用程序时杀了它之后,它仍然在主页上, 这是我的代码
main.dart
bool checkingKey;
Future<bool> checkKey() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool checkingKey=prefs.containsKey("jwt");
print("$checkingKey");
return checkingKey;
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
Paint.enableDithering = true;
await checkKey().then((value){
checkingKey=value;
});
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// bool check=checkKey().then((bool value) => true);
print("hello=$checkingKey");
return MaterialApp(
home: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
),
child: Scaffold(
resizeToAvoidBottomInset: false,
body: Container(
color: Color(0xffccffcc),
child:checkingKey==false?LoginPage():mainPage()
),
),
),
routes: <String,WidgetBuilder>{
'/home':(BuildContext context)=>mainPage(),
'/login':(BuildContext context)=>LoginPage(),
}
);
}
}
login_signup_Auth.dart
Future<void> attemptLogIn(String username, String password,BuildContext context) async {
///?final storage =parent_inherit.of(context);
///?var verify=storage.verify;
SharedPreferences prefs = await SharedPreferences.getInstance();
print("$username $password");
final http.Response res = await http.post(
"https://green-earth.herokuapp.com/signin",
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
// 'authorization':'Bearer '+
},
body: jsonEncode(<String, String>{
"email": username,
"password": password
}),
);
if(res.statusCode == 200) {
prefs.setString('jwt',res.body);
var value=prefs.getString('jwt');
print("storage= ${value.isEmpty}");
Navigator.of(context).pushNamed('/home');
}
else{
return _showMyDialoglogin(context,res.statusCode);
}
}
void logoutOutOfApp(BuildContext context) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.clear();
Navigator.of(context).pushNamedAndRemoveUntil('/login', (Route<dynamic> route) => false);
}
在不做任何更改的情况下第二次构建时,返回了检查键变量'true',我不知道,这怎么可能!!!!!!
我没有明白我做错了什么,如果您看到任何其他可以提高程序效率的问题或任何其他应该使用的代码。请告诉
非常感谢!!
你为什么要把事情复杂化?
您的 main.dart
可以简单地看起来像这样
bool checkingKey;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
Paint.enableDithering = true;
var prefs = await SharedPreferences.getInstance();
checkingKey = prefs.containsKey("jwt");
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("hello=$checkingKey");
return MaterialApp(
home: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
),
child: Scaffold(
resizeToAvoidBottomInset: false,
body: Container(
color: Color(0xffccffcc),
child: !checkingKey ? LoginPage() : mainPage(),
),
),
),
routes: <String,WidgetBuilder>{
'/home':(BuildContext context) => mainPage(),
'/login':(BuildContext context) => LoginPage(),
},
);
}
}