I m very new to firestore. So i have been trying to get my data back from firestore, but only a single field which is the userName field. However, I have no idea how to access just a single field inside a document.

My Result now :

flutter: Document data: {age: 50, email: [email protected], userName: Nicolo Big Boi}

The code :

Future getUserData() async {
    return userCollection
        .doc(uid)
        .get()
        .then((DocumentSnapshot documentSnapshot) {
      if (documentSnapshot.exists) {
        print('Document data: ${documentSnapshot.data()}');
      } else {
        print('Document does not exist on the database');
      }
    });
  }

But this is what I want :

flutter: Document data: { userName: Nicolo Big Boi}

Any help is very much appreciated, thank you.


Solution 1: Frank van Puffelen

To access a specific field of the DocumentSnapshot you can use:

print('Document data: ${documentSnapshot.data()["userName"]}')