2
votes

I'm attempting to get a list of all document names within a Firebase collection to display in a DropdownButton, for which I've created a custom widget, _DropdownListWithLabel. I have two documents in my collection (and I can see both in the Firebase console), but when I attempt to retrieve the names of all the documents as a list of strings, only one shows up. Here is the snippet of code doing this:

StreamBuilder<QuerySnapshot>(
    stream: Firestore.instance
        .collection('collectionName')
        .snapshots(),
    builder: (context, snapshot) {
        print(
            "Number of documents: ${snapshot.data.documents.length}, 
            ${snapshot.data.documents.first.documentID}");
        return _DropdownWithLabel(
            'Label', 
             snapshot.data.documents.map((doc) => doc.documentID).toList());
    }) //StreamBuilder

For reference, the print statement shows this: Number of documents: 1, documentName

Why might this code not be retrieving both document names from this collection?

Edit: here's the _DropdownWithLabel widget. If I try to list the document names in a ListView or a column with a list of Text widgets, I get the same result of only one document name being shown.

class _DropdownWithLabel extends StatefulWidget {
  final String label;
  final List<String> dropdownItems;
  _DropdownWithLabel(this.label, this.dropdownItems);
  @override
  State<StatefulWidget> createState() =>
      _DropdownWithLabelState(label, dropdownItems);
}

class _DropdownWithLabelState extends State {
  final String label;
  final List<String> dropdownItems;
  String selectedItem;
  _DropdownWithLabelState(this.label, this.dropdownItems) {
    selectedItem = dropdownItems.first;
  }
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        Text(label),
        Container(
          width: 150,
          child: DropdownButton<String>(
              value: selectedItem,
              onChanged: (newValue) {
                setState(() => selectedItem = newValue);
              },
              items: dropdownItems.map((value) {
                return DropdownMenuItem(
                  value: value,
                  child: Text(value),
                );
              }).toList()),
        )
      ],
    );
  }
}
2
can you show the _dropDownWithLabel widget ?Salma.
Yes - I edited the post to include thisConnor Szczepaniak

2 Answers

0
votes

The code looks just like mine, which is working. It depends on what your rules look like.

The error may be that the credentials included with the request don't have access to one of the documents. Setting the right permissions should solve the problem.

0
votes

I solved my problem by killing my emulator and restarting. Apparently something was stopping it from properly connecting to Firestore.