Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question related to custom User Model #13

Open
zhouhao27 opened this issue Apr 28, 2020 · 2 comments
Open

Question related to custom User Model #13

zhouhao27 opened this issue Apr 28, 2020 · 2 comments

Comments

@zhouhao27
Copy link

zhouhao27 commented Apr 28, 2020

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
Copy link

did you find the answer

@ChasingBugs101
Copy link

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! 👍 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants