[英]Flutter - null safety and models
I have returned to Flutter after while (in the meantime null safety was introduced).过了一会儿我又回到了Flutter(同时引入了null安全)。 I ended up with a need to update a project.
我最终需要更新一个项目。 Let's say in that project I have a User model. I managed to update most of the project but the part that gives me a headache is logout action.
假设在那个项目中我有一个用户 model。我设法更新了大部分项目,但让我头疼的部分是注销操作。 There is a need to clear the user (after logout) which I need to set up as null or empty it otherwise, but of course I am getting an error:
Unhandled Exception: type 'Null' is not a subtype of type 'User'
需要清除用户(注销后),我需要将其设置为 null 或将其清空,但我当然会收到错误消息:
Unhandled Exception: type 'Null' is not a subtype of type 'User'
So my question here is what is the best strategy to clear not only user but any other models I have for a redux state without hitting this problem with models not being able to be null?所以我的问题是什么是最好的策略,不仅要清除用户,还要清除我拥有的 redux state 的任何其他模型,而不会遇到模型不能成为 null 的问题?
User model:用户model:
class User {
String id;
String username;
String email;
String jwt;
User({ required this.id, required this.username, required this.email, required this.jwt });
factory User.fromJson(Map<String, dynamic> json) {
return User (
id: json['id'],
username: json['username'],
email: json['email'],
jwt: json['jwt']
);
}
}
User actions:用户操作:
/* User actions */
ThunkAction<AppState> getUserAction = (Store<AppState> store) async {
final prefs = await SharedPreferences.getInstance();
final String? storedUser = prefs.getString('user');
final user = storedUser != null ? User.fromJson(json.decode(storedUser)) : null;
if(user != null) {
store.dispatch(GetUserAction(user));
}
};
ThunkAction<AppState> logoutUserAction = (Store<AppState> store) async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove('user');
var user;
store.dispatch(LogoutUserAction(user));
};
class GetUserAction {
final User _user;
User get user => this._user;
GetUserAction(this._user);
}
class LogoutUserAction {
final User _user;
User get user => this._user;
LogoutUserAction(this._user);
}
NOTE: see how I managed to go about the null in the getUserAction (login) part.注意:看看我是如何在 getUserAction(登录)部分中找到关于 null 的 go 的。 I just don't dispatch the action if it is null, however I can not do this in the logout as I need explicitly to set the user to null (clear it) and that way log it out from the app.
如果它是 null,我只是不发送操作,但是我不能在注销中执行此操作,因为我需要明确地将用户设置为 null(清除它),然后从应用程序中注销它。
Can I make a model accept null values?我可以让 model 接受 null 值吗? How would I go about this?
我怎么会go这个呢? Or is there any other way I should go about this?
或者有什么其他方法我应该 go 吗? Thank you for your kind answer.
谢谢你的回答。
Change your model to:将您的 model 更改为:
class User {
String? id;
String? username;
String? email;
String? jwt;
User({ required this.id, required this.username, required this.email, required this.jwt });
factory User.fromJson(Map<String, dynamic> json) {
return User (
id: json['id'] ?? "",
username: json['username'] ?? "",
email: json['email'] ?? "",
jwt: json['jwt'] ?? ""
);
}
}
And you need check null_safety for all variable with operation??你需要检查所有带有操作的变量的 null_safety??
After logout you can check user null with user.id == ""
or user == User()
注销后,您可以使用
user.id == ""
或user == User()
检查用户 null
ThunkAction<AppState> logoutUserAction = (Store<AppState> store) async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove('user');
var user = User();
store.dispatch(LogoutUserAction(user));
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.