iamshaunjp / flutter-firebase

All course files for the Flutter & Firebase tutorial playlist on The Net Ninja YouTube channel
578 stars 440 forks source link

Question related to custom User Model #13

Open zhouhao27 opened 4 years ago

zhouhao27 commented 4 years ago

I want to add more fields to User model, for example, name, sex, job title, age, etc. Here is my code:

Stream<User> get user {
    // return _auth.onAuthStateChanged.map(_fromFirebaseUser);

    final theUser = _auth.onAuthStateChanged.map((firebaseUser) {
      final result = Firestore.instance.collection("users")
        .document(firebaseUser.uid).snapshots().map((snapshot) { 
           return User(
             uid: user.uid,
             name: snapshot.data['name'],
             email: user.email,
             age: snapshot.data['age'],
             gender: snapshot.data['gender'] 
          );
       }
      return result;
    });
    return theUser;
  }

The basic idea is I will get the data from users collection and populate the User model. But I got the following error message:

The argument type 'Stream<Stream>' can't be assigned to the parameter type 'Stream'.

Need your advice. Thanks.

bestshubhamever commented 3 years ago

did you find the answer

ChasingBugs101 commented 3 years ago

I think you could create another class service for getting user snapshot from Firestore and inside there you could create a function or method that accepts string parameter uid of the current user logged in. And has a returning value of stream user.

class FirestoreService { Stream getUserFromFirestore(String uid) { final user = Firestore.instance .collection("users") .document(uid) .snapshots() .map((snapshot) { return User( uid: snapshot.data['uid'], name: snapshot.data['name'], email: snapshot.data['email'], age: snapshot.data['age'], gender: snapshot.data['gender']); }); return user; } }

And also don't forget to modify first your User model with properties you want to add. Like this...

class User { final String uid; final String name; final String email; final int age; final String gender;

User({this.uid, this.name, this.email, this.age, this.gender}); }

Hope it helps and don't judge me if it doesn't work/not a perfect a solution. I'm also new to dart & flutter development. Good luck! 👍 😄