DART – how to combine two different Future based queries?

Issue

I have a Flutter form that will require data from two different sources, i.e.

class DataForForm {
   MasterData master;
   List<Transaction> transactions;
}

One query will bring back ‘master data’, and a second query will bring back ‘transaction data’.

How can I build a ‘Future’ call that will wait for both queries to execute, and for the results to be returned in the aggregated ‘DataForForm’ class?

Solution

Try something like this:

Future<DataForForm> getData(...) async {
  MasterData masterData = await getMasterData(...);
  List<Transaction> transactions = await getTransactions(...);
  return DataForm(master: masterData, transactions: transactions);
}

Answered By – TripleNine

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *