pika3113
pika3113

Reputation: 65

Dropdownbutton list not updating

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.

Upvotes: 0

Views: 42

Answers (1)

Timur
Timur

Reputation: 1343

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();
  }

Upvotes: 1

Related Questions