How to fix error to fill DropDown Button with Firestore

1,184 views
Skip to first unread message

Misael Astorga

unread,
Jun 7, 2019, 6:24:57 PM6/7/19
to Flutter Development (flutter-dev)

I want to fill a Dropdown Button with Firestore and i can get the data but i can't put it on the dropdown.


I had already identified the problem and it's the item list of the DropDown Button but i don't understand what is happening.

When i put the valor null on 'items' it works (doesn't show error) but doesn't show anything (obviously).


Here is my code.

StreamBuilder<QuerySnapshot>(
          stream: Firestore.instance
              .collection('usuarios/$id/ubications')
              .snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              const Text("Error");
            } else {
              List<DropdownMenuItem> ubicationItems = [];
              print(snapshot.data.documents.length);
              for (int i = 0; i < snapshot.data.documents.length; i++) {
                DocumentSnapshot snap = snapshot.data.documents[i];
                //_accountType.add(snap['ubication']);
                ubicationItems.add(DropdownMenuItem(
                  child: Text(
                    snap['ubication'],
                  ),
                  value: "${snap['ubication']}",
                ));
              }
              print(ubicationItems.length); //it has the data
              return Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  DropdownButton(
                    items: ubicationItems,
                    onChanged: (selectedType) {
                      print('$selectedType');
                      setState(() {
                        selectedType = selectedUbication;
                      });
                    },
                    value: selectedUbication,
                    isExpanded: true,
                    hint: new Text("ubication"),
                  ),
                ],
              );
            }
          }),


This is the error:
I/flutter (14877): Another exception was thrown: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 609 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true.

i used this example like guide: https://github.com/whatsupcoders/FlutterDropDown/blob/master/lib/main.dart

I hope someone can help me, thanks.

Angela Scott

unread,
May 6, 2020, 11:53:30 AM5/6/20
to Flutter Development (flutter-dev)
Hi did you find a solution to this as I am having the same problem
Reply all
Reply to author
Forward
0 new messages