Home > OS >  Instance members can't be accessed from a static method
Instance members can't be accessed from a static method

Time:06-08

This is the code:

class UserProvider with ChangeNotifier {
  User? _user;
  final AuthMethods _authMethods = AuthMethods();

  static User get getUser => **_user!;**

  Future<void> refreshUser() async {
    User user = await _authMethods.getUserDetails();
    _user = user;
    notifyListeners();
  }
}

I tried to call User but It had to be static so I made it static but now I have an issue with the _user!

CodePudding user response:

With prefix _ (underscore) it makes the member _user private, dart doesn't have private/public keywords.

In your case, in order to access the _user you can create a getter to access it.

You can also check the Libraries & Visibility section of dartlang.

Check more details here

CodePudding user response:

You can make _user as static field, and it will be accessible on static getter.

  static int? _user;

  static int get getUser => _user!;
  • Related