skip to Main Content

I am trying to populate a DropDownButton in my flutter app by pulling a list of users from the Firebase database but the snapshot is empty. I know there are items in the collection so I don’t know what is wrong.

Here is the code for the StreamBuilder:

StreamBuilder<QuerySnapshot>(
  stream: _db.collection('users').snapshots(),
  builder: (BuildContext context, AsyncSnapshot snapshot) {
    if (snapshot.hasData == true) {
      return const Center(
        child: CircularProgressIndicator(),
      );
    } else {
      final usersList = snapshot.data.docs;
      return DropdownButton<String>(
        hint: const Text("Select User"),
        value: _currentCompany,
        onChanged: changedDropDownCompany,
        items: snapshot.data.docs
            .map<DropdownMenuItem<String>>((document) {
          return DropdownMenuItem<String>(
            value: document.id,
            child: Text(document.data()['fName']),
          );
        }).toList(),
      );
    }
  }),

Here is a pic of the firebase:
enter image description here
This should work. Where have I gone wrong?

Thanks for your help

EDITS
Ok, so final usersList = snapshot.data.docs; populates usersList with the data but I am getting the error below:
enter image description here

I have modified the code as below:

StreamBuilder(
                    stream: _db.collection('users').snapshots(),
                    builder: (BuildContext context, AsyncSnapshot snapshot) {
                      if (snapshot.hasData) {
                        final usersList = snapshot.data.docs;
                        return DropdownButton<String>(
                          hint: const Text("Select User"),
                          value: _currentCompany,
                          onChanged: changedDropDownCompany,
                          items: usersList
                              .map<DropdownMenuItem<String>>((usersList) {
                            return DropdownMenuItem<String>(
                              value: usersList.id.toString(),
                              child: Text(usersList.data()['fName']),
                            );
                          }).toList(),
                        );
                      } else {
                        return const CircularProgressIndicator();
                      }
                    }),

If I try to evaluate the line "child: Text(UsersList.data()[‘fName’])," it says it can not evaluate the line.

So, I guess now I need to know how do I pulled the data out of the snapshot to add to the items: ?

2

Answers


  1. You are getting the logic wrong. if snapshot.hasData then show your dropdown

    StreamBuilder<QuerySnapshot>(
                    stream: _db.collection('users').snapshots(),
                    builder: (BuildContext context, AsyncSnapshot snapshot) {
                      if (snapshot.hasData) {
                        final usersList = snapshot.data.docs;
                        return DropdownButton<String>(
                          hint: const Text("Select User"),
                          value: _currentCompany,
                          onChanged: changedDropDownCompany,
                          items: snapshot.data.docs
                              .map<DropdownMenuItem<String>>((document) {
                            return DropdownMenuItem<String>(
                              value: document.id,
                              child: Text(document.data()['fName']),
                            );
                          }).toList(),
                        );
                      } else {
                       return const CircularProgressIndicator();
                      }
                    }),
    
    Login or Signup to reply.
  2. As mentioned by CharlyKeleb, "snapshot.hasData == true" is incorrect.

    I would also suggest not to not stream "_db.collection(‘users’).snapshots()" directly. but instead assign it like:

    var _stream;
    
    @override
      void initState() {
        _stream = _db.collection('users').snapshots();
      }
    

    Also trying changing

    Text(usersList.data()['fName'])
    to
    Text(usersList['fName'])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search