DitIsTijn
DitIsTijn

Reputation: 21

Firestore Class 'QuerySnapshot' has no instance method '[]'

I want a ListView to show the names of the users. I am using a cloudfunction with the admin sdk to return a list of all the users with the corresponding user IDs. When I want to pass that uid to a Widget with a streambuilder, it gives me the error:

Class 'QuerySnapshot' has no instance method '[]'.
Receiver: Instance of 'QuerySnapshot'
Tried calling: []("firstName")

This is the function I am calling while building the ListView for the title:

Widget getFirstName(uid, item) {
  return StreamBuilder(
    stream: Firestore.instance
        .collection('users')
        .document('HzBUMs06BAPHK0Y7m5kfOmUzawC2')
        .collection('userInfo')
        .snapshots(),
    builder: (BuildContext context, AsyncSnapshot snapshot) {
      if (!snapshot.hasData) {
        return Text('${item['email']}');
      } else {
        return Text('${snapshot.data.documents['firstName']}');
      }
    },
  );
}

I am not using the uid which I will pass to it yet, as the User ID that I hardcoded right now is the only one with the firstName data in it.

When I feed it a non-existing userID, it still seems to think it has data in it and tries to return its (non-existent) data.

What am I doing wrong here?

Upvotes: 1

Views: 499

Answers (1)

DitIsTijn
DitIsTijn

Reputation: 21

I managed to fix it by using this piece of code:

Widget fullNameWidget(uid) {
  return FutureBuilder(
    future: fullName(uid),
    builder: (context, snapshot) {
      return Text('${snapshot.data}');
    },
  );
}

Future fullName(uid) async {
  return Firestore.instance
      .collection("users")
      .document('$uid')
      .collection("userInfo")
      .getDocuments()
      .then((querySnapshot) {
    print(querySnapshot.documents[0]['firstName']);
    if (querySnapshot == 'null') {
    } else {
      return '${querySnapshot.documents[0]['firstName']} ${querySnapshot.documents[0]['lastName']}';
    }
    // querySnapshot.documents.where((element) {
    //   print(element.documentID == 'firstName');
    // });
  });
}

Upvotes: 1

Related Questions