我想在用户成功登录 flutter 后保留用户令牌。我正在尝试将设备令牌保存到 SharedPreferences,以便我可以在其他小部件中使用它,但错误始终是:无效参数(值) : 不能为空。如何保存 ID 和访问令牌?
我的代码:
root = tk.Tk()
figure1 = plt.Figure(figsize=(2,2), dpi=100)
ax1 = figure1.add_subplot(221)
ax1.plot(df1['year'], df1['personal'], color='red')
scatter1 = FigureCanvasTkAgg(figure1, root)
scatter1.get_tk_widget().pack()
ax1.legend([''])
ax1.set_xlabel('valeur de personals')
ax1.set_title('ev de personal ')
figure2 = plt.Figure(figsize=(2,2), dpi=100)
ax2 = figure2.add_subplot(222)
scatter2 = FigureCanvasTkAgg(figure2, root)
scatter2.get_tk_widget().pack(side=tk.RIGHT)
ax2.legend([''])
ax2.set_xlabel('valeur BSA')
ax2.set_title('Evolutiion des valeurs BSA depuis 1990 ')
ax2.plot(df2['year'], df2['value'], color='red')
figure3 = plt.Figure(figsize=(2,2), dpi=100)
ax3 = figure3.add_subplot(223)
#the same code for the reste
root.mainloop()
还有用户模型:
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);
}
}
答案 0 :(得分:0)
首先检查您要保存的值是否不为空且 setInt
或 setString
是 async
函数,因此您需要在它们之前添加 await
>
Future<void> _saveUser(int id, String access_token) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.setInt('id', id);
await sharedPreferences.setString('access_token', access_token);
}
如果您已经注意到,您的 User
类不会返回 .fromJson
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]);
// return a User object here
factory User.fromJson(Map<String, dynamic> 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.adress = jsonObject['adress'];
this.email_verifed_at = jsonObject['email_verifed_at'];
this.username = jsonObject['username'];
});
}