颤音 - 无效的安全性和模型

发布于 2025-01-29 21:04:09 字数 1607 浏览 1 评论 0原文

一段时间以后,我回到了颤音(同时引入了无效的安全性)。我最终需要更新项目。假设我有一个用户模型。我设法更新了项目的大部分,但是让我头痛的部分是注销动作。有必要清除用户(注销后),我需要将其设置为null或否则将其设置为空,但是我当然会遇到错误: 未经治疗的例外:type'null'不是类型'用户''的

亚型模型无法无效的问题?

用户模型:

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 */
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);
}

注意:查看我如何设法绕过getUseraction(登录)部分中的空。如果操作为null,我只是不派遣操作,但是我无法在注销中执行此操作,因为我需要明确将用户设置为null(清除它),并从应用程序中将其登录。

我可以让模型接受零值吗?我该怎么做?还是我应该做其他方法?谢谢您的回答。

I have returned to Flutter after while (in the meantime null safety was introduced). 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. 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'

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?

User 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. 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.

Can I make a model accept null values? How would I go about this? Or is there any other way I should go about this? Thank you for your kind answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请你别敷衍 2025-02-05 21:04:09

将模型更改为:

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'] ?? ""
    );
  }
}

您需要检查所有变量使用操作的NULL_SAFETY吗?

注销后,您可以使用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));
};

Change your model to:

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 ??

After logout you can check user null with user.id == "" or user == User()

ThunkAction<AppState> logoutUserAction = (Store<AppState> store) async {
  final prefs = await SharedPreferences.getInstance();
  await prefs.remove('user');
  var user = User();
  store.dispatch(LogoutUserAction(user));
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文