return a list from a future in flutter return a list from a future in flutter flutter flutter

return a list from a future in flutter


I would recommend either a FutureBuilder or StreamBuilder (for streams) which gives you layout performance benefits and also tools to easily add loading and error widgets.It could look like following:

Future<List<User>> usersFuture = getUsers('DcofOiHWcjbjD0i18miW');

Create the future as member variable so you only fetch once (in case the method initiates a new future each time you call it). And then use it inside a FutureBuilder.

FutureBuilder<List<User>>(    future: usersFuture,    builder: (context, snapshot) {      if(snapshot.connectionState != ConnectionState.done) {        // return: show loading widget      }      if(snapshot.hasError) {        // return: show error widget      }      List<User> users = snapshot.data ?? [];      return ListView.builder(        itemCount: users.length,        itemBuilder: (context, index) {          User user = users[index];          return new ListTile(            leading: CircleAvatar(              backgroundImage: AssetImage(user.profilePicture),            ),            trailing: user.icon,            title: new Text(user.name),            onTap: () {              Navigator.push(context,                  new MaterialPageRoute(builder: (context) => new Home()));            },          );      });  });


Future<Solution> JuniorDeveloperQuestion {// use Expansion List with Dynamic Datareturn _2021.Solution.ExpansionListWithDynamicData; }

https://dev.to/rrtutors/dynamic-expansiontile-create-expansion-list-with-dynamic-data-2hl3