Home > OS >  Flutter: struggle with async functions
Flutter: struggle with async functions

Time:12-22

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

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

CodePudding user response:

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