Home > Back-end >  Dropdownbutton list not updating
Dropdownbutton list not updating

Time:08-10

final activities = ['select'];
@override
  void initState() {
    FirebaseFirestore.instance.collection('activities').get().then(
          (snapshot) => snapshot.docs.forEach((document) {
            activities.add(document.reference.id);
          }),
        );
    super.initState();
  }

I have a dropdownbutton, and it uses 'activities' as a list, however, when the page loads it only shows 'select' as an option, and only after selecting select, and then clicking the drop down will the updated list show up.

CodePudding user response:

Your activities that you are getting from firebase is probably adding after page load. So your activities is getting updated but after page load and you can't see the changes. So you need to setState when activities updated.

final activities = ['select'];
@override
  void initState() {
    FirebaseFirestore.instance.collection('activities').get().then(
          (snapshot) {
              snapshot.docs.forEach((document) {
            activities.add(document.reference.id);
          });
          setState((){});
          }
        );
    super.initState();
  }
  • Related