Home > Blockchain >  Non-nullable instance field '_user' must be initialized
Non-nullable instance field '_user' must be initialized

Time:09-26

Non-nullable instance field '_user' must be initialized. & The argument type 'Future Function(User)' can't be assigned to the parameter type 'void Function(User?)?

class UserProvider with ChangeNotifier {
FirebaseAuth _auth;
User _user;
Status _status = Status.Uninitialized;
FirebaseFirestore _firestore = FirebaseFirestore.instance;
UserServices _userServices = UserServices();
// OrderServices _orderServices = OrderServices();
UserModel _userModel = UserModel();

 //  getter
  UserModel get userModel => _userModel;
  Status get status => _status;
  User get user => _user;

 UserProvider.initialize() : _auth = FirebaseAuth.instance {
 _auth.authStateChanges().listen(_onStateChanged);
 }

Future<void> _onStateChanged(User firebaseUser) async {
 if (firebaseUser == null) {
   _status = Status.Uninitialized;
 } else {
   _user = firebaseUser;
   _status = Status.Authenticated;
   _userModel = await _userServices.getUserById(firebaseUser.uid);
  }
  notifyListeners();
}

CodePudding user response:

idk what's your code or what are you tring to do but the _user variable should be initialized in initState()

CodePudding user response:

Can you please try this . Please add ? befor _user in declaration like this. User ? _user;

  • Related