简体   繁体   中英

How to get Single document from firestore and call the fields in UI in flutter/dart

Here is my attempt

In my Controller I have this

class UserController extends GetxController {
  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

  var _proo;
  get prooo => _proo;

  Future<Member?> readProfile() async {
       _proo = FireStoreHelper().fFetch("users", "user1");
  }

}

In my FireStoreHelper I have this

class FireStoreHelper {

  fFetch(collection, doc) {
    final docMember =
        FirebaseFirestore.instance.collection(collection).doc(doc);
    var query = docMember.get();
    return query;
  }

This is my Model

class Member {
 
  final String? username;
  //...others
  Member({

    this.username,
  //...others
  });


  static Member fromJson(Map<String, dynamic> json) => Member(
     
        username: json['username'],
        //...others
      );
}

Then in my UI I have this

   Get.lazyPut(() => UserController().readProfile());

    return GetBuilder<UserController>(builder: (userController) {

   //.......

 Text(userController.prooo.username),

}

Actually what am trying get a username of user1 as seen in the Image below

在此处输入图像描述

Please help me, I am new to this.

try this one...

fFetch(collection, doc) async {

    final docMember = await
       FirebaseFirestore.instance.collection(collection).doc(doc).get();

    return docMember;

}


static Future<Member?> readProfile() async {

    _proo = await FireStoreHelper().fFetch("users", "user1");

    Member member = Member.fromJson(_proo);

    return member;

}

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