Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter: struggle with async functions

What I want to do is to load users and put them in a list. I am doing this with the function getUsers().
After that I want to load an Event (with fetchEvent()) which is a table with some dropdown menus.
My problem is that the users are sometimes not there when the event gets loaded. Thats why dropdown button is disabled.
I tried to solve this with using "then".

This is my initState:

void initState() {

    besetzungsList.clear();

    if (widget.eventId != null) {
      getUsers().then((_) => fetchEvent());
    } else
      getUsers();

    print(widget.eventId);

    super.initState();
  }

This is getUsers()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  getUsers() async {
    getCurrentUser();
    usersStream = authBloc.getUsers();
    usersStream.listen((allUsers) {
      for (ApplicationUser user in allUsers) {
        if (user.userRoleList.contains(team)) users.add(user.username);
      }
    });
  }
 fetchEvent() async {

   ...

        dataTableRows.add(DataRow(
          cells: [
            DataCell(
              Text(event.dienstMap["Aufgabe"][i]),
              onTap: () {
                removeRow(i);
              },
            ),
            DataCell(
              Text(event.dienstMap["Zeit"][i]),
              onTap: () {},
            ),
            DataCell(
              Text(event.dienstMap["Team"][i]),
              onTap: () {},
            ),
            DataCell((i > event.dienstMap["Besetzung"].length - 1 == true)
                ? DropDown(
                    hint: "Wählen",
                    users: users,
                    besetzungsListChanged: (String value) {
                      if (besetzungsList.length > 0) {
                        besetzungsList.removeAt(i);
                      }

                      besetzungsList.insert(i, value);
                    },
                    fromDropDown: (bool value) => fromDropDown = value,
                  )
                : DropDown(
                    hint: event.dienstMap["Besetzung"][i],
                    users: users,
                    besetzungsListChanged: (String value) {
                      besetzungsList.removeAt(i);
                      besetzungsList.insert(i, value);
                    },
                    fromDropDown: (bool value) => fromDropDown = value,
                  ))
          ],
        ));
      }
    });
    setState(() {});
  }

>Solution :

You can add that fetchEvent in your stream listener, so that it will get called after adding the user object to the array. Since it’s a stream if you get a new user object, it will again call that fetchEvent and update your dropdown values.

Future<void> getUsers() async {
   getCurrentUser();
   usersStream = authBloc.getUsers();
   usersStream.listen((allUsers) {
      for (ApplicationUser user in allUsers) {
         if (user.userRoleList.contains(team)) {
            users.add(user.username);
         }
      }
      await fetchEvent();
   });
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading