【发布时间】:2021-02-01 12:56:48
【问题描述】:
目前,如果用户成功登录一次,我已启用“继续登录”功能。但是,我仍然想制作一个“记住我”复选框来保存用户的成功登录信息。谁能帮我解决这个问题?
需要:如果用户成功登录一次,则允许用户存储电子邮件和密码的复选框。
代码如下:
signIn(String email, pass) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String url = ServerDetails.ip +
':' +
ServerDetails.port +
ServerDetails.api +
'login';
Map<String, String> headers = {"Content-type": "application/json"};
var data = jsonEncode({
'email': email,
'password': pass,
'token': FirebaseNotifications.fcmtoken
});
var jsonResponse = null;
var response = await http.post(url, headers: headers, body: data);
if (response.statusCode == 200) {
jsonResponse = json.decode(response.body);
if (jsonResponse != null) {
setState(() {
_isLoading = false;
});
sharedPreferences.setString("token", jsonResponse['token']);
sharedPreferences.setString(
"token_expire_date", jsonResponse['token_expire_date']);
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (BuildContext context) => MainPage()),
(Route<dynamic> route) => false);
}
} else {
setState(() {
_isLoading = false;
});
Widget okButton = FlatButton(
child: Text("OK"),
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => MainPage()));
});
setState(() {
AlertDialog alert = AlertDialog(
title: Text("Error message"),
content: Text("Oops! The password is wrong or the email is invalid."),
actions: [
okButton,
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
});
print(response.headers);
print(response.body);
}
}
【问题讨论】:
-
你想保存什么样的信息?
-
@Creator 如果用户单击复选框并成功登录一次,则保存用户名(在本例中显示为电子邮件)和密码。
-
我不建议您这样做,因为这会使您的应用极易受到黑客攻击。而且您的用户使用您的应用程序会感到不安全。我建议您只保存用户名或电子邮件。
-
@Creator 感谢您的提醒!但我仍然想知道在我的情况下该怎么做。
-
首先创建一个记住我的按钮。然后是地图列表。其中包含每个用户登录到您的应用程序的用户名和密码。每次新用户登录时都会保存他的信息。