Flutter 如何使用共享首选项在Flatter中保存令牌?
我想在用户成功登录到Flatter后保留用户令牌。我正在尝试将设备令牌保存到SharedReferences,以便在其他小部件中使用它,但错误始终为:无效参数(值):不得为null。如何保存ID和访问令牌 我的代码:Flutter 如何使用共享首选项在Flatter中保存令牌?,flutter,dart,Flutter,Dart,我想在用户成功登录到Flatter后保留用户令牌。我正在尝试将设备令牌保存到SharedReferences,以便在其他小部件中使用它,但错误始终为:无效参数(值):不得为null。如何保存ID和访问令牌 我的代码: Future<User> login(String username, String password) async { await checkInternet(); Map<String, String> headers = {
Future<User> login(String username, String password) async {
await checkInternet();
Map<String, String> headers = {
'Content-type': 'application/json',
'Accept': 'application/json',
// 'Authorization': 'Bearer $token'
};
Map<String, String> body = {'username': username, 'password': password};
var response = await http.post(Uri.parse(ApiUtil.AUTH_LOGIN),
headers: headers, body: jsonEncode(body));
switch (response.statusCode) {
case 200:
var body = jsonDecode(response.body);
// print(body);
var data = body['user'];
// print(data);
User user = User.fromJson(data);
await _saveUser(user.id, user.access_token);
return user;
case 404:
throw ResourceNotFound('User');
break;
case 422:
throw UnProcessedEntity();
break;
case 401:
throw LoginFailed();
default:
return null;
break;
}
}
Future<void> _saveUser(int id, String access_token) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.setInt('id', id);
sharedPreferences.setString('access_token', access_token);
}
}
未来登录(字符串用户名、字符串密码)异步{
等待检查互联网();
映射头={
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”,
//“授权”:“持票人$token”
};
映射体={'username':用户名,'password':密码};
var response=wait http.post(Uri.parse(ApiUtil.AUTH_LOGIN),
headers:headers,body:jsonEncode(body));
开关(响应状态代码){
案例200:
var body=jsonDecode(response.body);
//印刷品(正文);
var数据=主体[‘用户’];
//打印(数据);
User=User.fromJson(数据);
wait_saveUser(user.id、user.access_令牌);
返回用户;
案例404:
抛出ResourceNotFound('User');
打破
案例422:
抛出未处理的内容();
打破
案例401:
抛出登录失败();
违约:
返回null;
打破
}
}
Future\u saveUser(int-id,字符串访问\u令牌)异步{
SharedReferences SharedReferences=等待SharedReferences.getInstance();
setInt('id',id');
setString('access\u token',access\u token);
}
}
rhis是用户模型:
class User {
int id;
String name;
String email;
String username;
String phone;
String access_token;
String activeboxe_id;
String adress;
DateTime email_verifed_at;
User(this.name, this.email, this.username, this.phone, this.activeboxe_id,
this.adress, this.email_verifed_at,
[this.access_token, this.id]);
User.fromJson(Map<String, dynamic> jsonObject) {
this.id = jsonObject['id'];
this.name = jsonObject['name'];
this.email = jsonObject['email'];
this.phone = jsonObject['phone'];
this.access_token = jsonObject['access_token'];
this.activeboxe_id = jsonObject['activeboxe_id'];
this.adress = jsonObject['adress'];
this.email_verifed_at = jsonObject['email_verifed_at'];
this.username = jsonObject['username'];
}
}
类用户{
int-id;
字符串名;
字符串电子邮件;
字符串用户名;
字符串电话;
字符串访问令牌;
字符串activeboxe_id;
字符串地址;
DateTime电子邮件地址为;
用户(this.name、this.email、this.username、this.phone、this.activeboxe\u id、,
这个地址,这个电子邮件地址,
[this.access_令牌,this.id]);
fromJson(映射jsonObject){
this.id=jsonObject['id'];
this.name=jsonObject['name'];
this.email=jsonObject['email'];
this.phone=jsonObject['phone'];
this.access_token=jsonObject['access_token'];
this.activeboxe_id=jsonObject['activeboxe_id'];
this.address=jsonObject['address'];
this.email_verifed_at=jsonObject['email_verifed_at'];
this.username=jsonObject['username'];
}
}
首先检查您试图保存的值是否不为空,并且setInt
或setString
是一个async
函数,因此您需要在它们之前添加wait
Future\u saveUser(int-id,字符串访问\u令牌)异步{
SharedReferences SharedReferences=等待SharedReferences.getInstance();
等待SharedReferences.setInt('id',id);
wait sharedPreferences.setString('access\u token',access\u token');
}
如果您注意到,您的用户
类不会返回.fromJson
类用户{
int-id;
字符串名;
字符串电子邮件;
字符串用户名;
字符串电话;
字符串访问令牌;
字符串activeboxe_id;
字符串地址;
DateTime电子邮件地址为;
用户(this.name、this.email、this.username、this.phone、this.activeboxe\u id、,
这个地址,这个电子邮件地址,
[this.access_令牌,this.id]);
//在此处返回用户对象
工厂用户.fromJson(映射jsonObject)=>User({
this.id=jsonObject['id'];
this.name=jsonObject['name'];
this.email=jsonObject['email'];
this.phone=jsonObject['phone'];
this.access_token=jsonObject['access_token'];
this.activeboxe_id=jsonObject['activeboxe_id'];
this.address=jsonObject['address'];
this.email_verifed_at=jsonObject['email_verifed_at'];
this.username=jsonObject['username'];
});
}
构造函数不能有返回类型。如何返回用户对象?根据您的问题,id值似乎为空。请检查您从响应中获得的值。我检查了它。id=响应的10您可以在响应200响应之后存储该值。您的问题将得到解决。访问令牌属性在用户模型之外。它在身体里。如何正确保存它?id很好Raju Gupta对我来说你的代码很完美。请再次验证ID和令牌的值,并检查运行时类型。