简体   繁体   中英

How can I display on a widget the current user data from Firebase to Flutter

How to display the data submitted by the current user on a widget? I already tried with streamBuilder and FutureBuilder and nada.

Firebase Users data

Screen where the current user data should be displayed

I could manage to displayed the data (country, username, email, password) from the first Firebase document by using: docs[0]['country'] but that's not right... It needs to be displaying the data from the current user and not always the data from the first document.

I already tried ListView.builder(), but when you scroll down you'll see all data as a list from all users and that's wrong too.

I look forward to seeing your answers. Thanks in advance for the help guys.

As i understand you want to take data about current or any user. First of all detect userId and you can work this code(I wrote very simple code for my project and you can apply to your code.)

This is Model Class

class MyUser{
  String snapshotId;
  String userId;
  String name;
  MyUser({this.userId,this.snapshotId,this.name});
  factory MyUser.fromSnaphot(DocumentSnapshot snapshot){
    return MyUser(
      snapshotId:snapshot.id,
      userId:snapshot.data()['userId'],
      name:snapshot.data()['name'],
      );
  }

This is Service Class

  final FirebaseFirestore _fBaseFireStore=FirebaseFirestore.instance;
  CollectionReference _collectionRef;
  UserService(){
    _collectionRef=_fBaseFireStore.collection('Users');
  }
  Stream<MyUser> getUserFromUserId(String userId){
    var ref=_collectionRef.where('userId',isEqualTo:userId);
    return ref.snapshots().map((event) => MyUser.fromSnaphot(event.docs[0]));
  }

Get User Function

final UserService _userService = getIt<UserService>();
return _userService.getUserFromUserId(userId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM