Home > database >  Firebase Stream using authStateChanges, wont return my custom model?
Firebase Stream using authStateChanges, wont return my custom model?

Time:11-05

I believe i have misunderstood the ? operator, and I'm not sure how to return a valid custom model. My reasoning to think this, is my "Customuser?" function, cant return null, which i dont understand? I would think it could catch a null reference.

My current attempt, looks like this:

final FirebaseAuth _auth = FirebaseAuth.instance;

//create user obj based on firebase user
Customuser? _customuser(User user) {
  return user != null ? Customuser(uid: user.uid) : null;
}

//auth change user stream
Stream<Customuser?> get streamedUser {
  return _auth.authStateChanges().map(_customuser);
}

The custom model simply looks like this:

class Customuser {
  final String? uid;

  Customuser({this.uid});
}

I'm not sure how to handle the ? operator on my custom model, so that it works with my stream.

The error message is attached.

Error message

CodePudding user response:

try to add ? to your User param and try again

Customuser? _customuser(User? user) {
  return user != null ? Customuser(uid: user.uid) : null;
}

  • Related