Home > Software engineering >  saving a value from a document snapshot from firestore
saving a value from a document snapshot from firestore

Time:11-02

in the following code i want to save the String data that i get from firestore inside the .then() method into another variable. but once its outside the method i lose the value.

String memberName = '';

    member.user.get().then(
      (DocumentSnapshot documentSnapshot) {
        if (documentSnapshot.exists) {
          memberName = documentSnapshot.get('Full_Name');
          print(memberName) //'John Smith'
        }
      },
    );

print(memberName) //''

posting all the code for reference.

 Widget buildGroupMemberTile(GroupMember member, bool loggedUserIsAdmin) {
    final memberDoc = FirebaseFirestore.instance
        .collection('groups')
        .doc(widget.group.id)
        .collection("members")
        .doc(member.email);
    print(getMemberName(member));

String memberName = '';

    member.user.get().then(
      (DocumentSnapshot documentSnapshot) {
        if (documentSnapshot.exists) {
          memberName = documentSnapshot.get('Full_Name');
          print(memberName) //'John Smith'
        }
      },
    );

print(memberName) //''

    return ListTile(
        onTap: (() {
          if (loggedUserIsAdmin) {
            if (member.role == 'admin') {
              memberDoc.update({'role': 'member'});
            } else if (member.role == 'member') {
              memberDoc.update({'role': 'admin'});
            }
          }
        }),
        title: Center(
          child: Padding(
            padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
            child: Container(
              width: 450,
              decoration: BoxDecoration(
                  color: Color.fromARGB(255, 65, 61, 82),
                  borderRadius: BorderRadius.all(Radius.circular(12))),
              child: Padding(
                padding: const EdgeInsets.fromLTRB(40, 20, 40, 20),
                child: Column(
                  children: [
                    Text(
                      memberName,
                      style: GoogleFonts.poppins(
                        color: ThemeColors.whiteTextColor,
                        fontSize: FontSize.large,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ));
  }

Im guessing that I'm referencing documentSnapshot.get('Full_Name') and not copying its the value to memberName. How can i keep the value?

CodePudding user response:

try to get it like this without the then, and call the data() first to get the Map<String, dynamic> of the docuemnt:

 DocumentSnapshot documentSnapshot =  await member.user.get();
   if (documentSnapshot.exists) {
      memberName = (documentSnapshot.data() as Map<String, dynamic>)["Full_Name"];
      print(memberName);
    }

Note: you need to mark the method where this is implemented to Future with async keyword

CodePudding user response:

Looks like you need to put the entire member.user.get() call into a async future method & place an await keyword just before the call.

Then you need to use a FutureBuilder around the widget that will be displaying the name to ensure the name has a value before it is rendered.

  • Related