wendux / flutter_in_action_source_code

《Flutter实战》随书源码
794 stars 226 forks source link

空安全问题? #26

Open YuQi9797 opened 1 year ago

YuQi9797 commented 1 year ago

https://github.com/wendux/flutter_in_action_source_code/blob/e5e54b3082bc344fb9c236a9e24796ac568ac859/github_client_app/lib/states/profile_change_notifier.dart#L14

大佬您好,关于此处: User get user => _profile.user; 由于_profile.userUser? 类型的, 所以在get的返回类型中改为了 User? 但当改好后User? get user => _profile.user; 又出现问题提示:The return type of getter 'user' is 'User?' which isn't a subtype of the type 'User' of its setter 'user'.

请问这种该如何解决呢? 谢谢!

YuQi9797 commented 1 year ago

这样吗?

class UserModel extends ProfileChangeNotifier {
  User? get user => _profile.user;

  // APP是否登录(如果有用户信息,则证明登录过)
  bool get isLogin => user != null;

  // 用户信息发生变化,更新用户信息并通知依赖它的子孙Widgets更新
  set user(User? user) {
    if (user?.login != _profile.user?.login) {
      _profile.lastLogin = _profile.user?.login;
      _profile.user = user;
      notifyListeners();
    }
  }
}