user17784209
user17784209

Reputation:

The argument type 'AppUser? Function(User)' can't be assigned to the parameter type 'User Function(User?)'

I get this error: The argument type 'AppUser? Function(User)' can't be assigned to the parameter type 'User Function(User?)'.

I found another stackoverflow Question about this topic, but the solution doesn´t work for me

here is the part of my code where the error appears:

  AppUser? _userFromFirebaseUser(User user) {
    return user != null ? AppUser(uid: user.uid) : null;
  }

  Stream<User> get user {
    return _auth.authStateChanges().map(_userFromFirebaseUser);
  }

Upvotes: 0

Views: 186

Answers (1)

h8moss
h8moss

Reputation: 5020

I wish you explained better what you are trying to do, but here is why this piece of code doesn't work:

Stream<User> get user { ... }

The values of your stream are of type User.

You are trying to map each user's values using this function:

AppUser? _userFromFirebaseUser(User user) { ... }

But that function doesn't return a User, it returns a AppUser?, very different things, you can't add a AppUser? to a Stream<User>. To fix this, change the type of your stream:

Stream<AppUser?> get user { ... }

Finally, I want you to know that user != null ? AppUser(uid: user.uid) : null; is redundant because user will never be null, user's type is User not User?, so you can actually completely get rid of nullable types:

AppUser _userFromFirebaseUser(User user) {
  return AppUser(uid: user.uid);
}

Stream<AppUser> get user {
    return _auth.authStateChanges().map(_userFromFirebaseUser);
}

On the off chance that user can be null, you will get an error with the above code, then you actually need to do the opposite of what I did, make everything nullable:

AppUser? _userFromFirebaseUser(User? user) {
  return user == null ? null : AppUser(uid: user.uid);
}

Stream<AppUser?> get user {
    return _auth.authStateChanges().map(_userFromFirebaseUser);
}

Upvotes: 1

Related Questions