vwdewaal
vwdewaal

Reputation: 1005

Flutter firebase user problem trying to link to a model

the error I'm getting:

A value of type 'UserA?' can't be returned from the method '_userFromFirebaseUser' because it has a return type of 'User'

I'm following along this tutorial: Flutter & Firebase App Tutorial #6 - Custom User Model

At about 5:35 into the video he talks about returning the Firebase user object and linking it to the model. Now the video is about 2 years old, so Firebase has moved on a bit. FirebaseUser is now just called User, but I think in my changes I've made a mistake. Here is my code:

class AuthService{
  final FirebaseAuth _auth = FirebaseAuth.instance;

  //create user object based on firebaseuser
  User _userFromFirebaseUser(User? user) {
    return user != null ? UserA(uid: user.uid) : null;
  }

This is my model:

class UserA {
   final String uid;

  UserA({ required this.uid });
}

Since FirebaseUser is now just called User I've changed the model to be UserA since I was getting confused.

Any suggestion on what I'm doing wrong?

Upvotes: 0

Views: 211

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599206

You're mixing up the two types of users in your code.

The video has two types: FirebaseUser and User, which you have mapped to Users and UserA respectively.

With your types, the function should be:

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

Aside from the type change, the ? is needed in Dart nowadays to indicate that you may return either a UserA object or null.

Upvotes: 1

Related Questions