Home > OS >  How can I return Stream<UserEntity> from firebase?
How can I return Stream<UserEntity> from firebase?

Time:12-17

I want to return user data from firebase as a stream as UserEntity I have this but it doen't work. Can you help me?

  @override
  Stream<UserEntity> getSingleUser(String uid) {
   final user= firebaseFirestore.collection('users').where("uid", isEqualTo: uid);
    return user.snapshots().map((event) => event.docs.map((e) => UserModel.fromSnapshot(e));
  }

CodePudding user response:

Get a reference to the document, not inside the query.

  Stream<UserEntity> getSingleUser(String uid) {
    final user = firebaseFirestore.collection('users').doc(uid);
    return user.snapshots().map((snapshot) => UserModel.fromSnapshot(snapshot));
  }

CodePudding user response:

You need to call data() on docs to get that data you want:

@override
  Stream<UserEntity> getSingleUser(String uid) {
   final user= firebaseFirestore.collection('users').where("uid", isEqualTo: uid);
    return user.snapshots().map((event) => event.docs.map((e) => UserModel.fromSnapshot(e.data()));//<--- change this
  }
  • Related