I have a couple of GET requests to make from flutter to an API, optimally I would like to have this run as efficiently as possible. I'm still learning Flutter, and coming from a JS background, I went with a pattern similar to Promise.all([ ... ]) in Javascript but I feel like this is a hack rather than a solution.

If there is a better approach to this, I look forward to reading about it:

final apiProvider = new ApiProvider();

final apiData = await Future.wait([
  apiProvider.getProducts(),   // Returns Future<List<Product>>
  apiProvider.getCategories(), // Returns Future<List<Category>>
  apiProvider.getBrands(),     // Returns Future<List<Brand>>
  apiProvider.getCustomers(),  // Returns Future<List<Customer>>
]);

List<Product> products    = apiData[0];
List<Category> categories = apiData[1];
List<Brand> brands        = apiData[2];
List<Customer> customers  = apiData[3];


Solution 1: jamesdlin

Using Future.wait is appropriate. I personally would await a list of Future<void>s that directly set your variables, and that would avoid the need to use indices later (and thus avoid potential mistakes) and also improve type-safety:

final apiProvider = ApiProvider();

List<Product> products;
List<Category> categories;
List<Brand> brands;
List<Customer> customers;

await Future.wait([
  Future<void>(() async => products = await apiProvider.getProducts()),
  Future<void>(() async => categories = await apiProvider.getCategories()),
  Future<void>(() async => brands = await apiProvider.getBrands()),
  Future<void>(() async => customers = await apiProvider.getCustomers()),
]);