0

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?

user2868835
  • 1,250
  • 3
  • 19
  • 33

1 Answers1

0

Try something like this:

Future<DataForForm> getData(...) async {
  MasterData masterData = await getMasterData(...);
  List<Transaction> transactions = await getTransactions(...);
  return DataForm(master: masterData, transactions: transactions);
}
TripleNine
  • 1,457
  • 1
  • 4
  • 15
  • thanks for the response, but with your sample code the two queries will run one after the other. Is there a way to have both executed in parallel? Like 'Future.wait' – user2868835 Sep 16 '22 at 17:24
  • What are you trying to achieve? One of these both queries has to be performed before the other one. Future.wait does the same just for a list of futures instead of two. – TripleNine Sep 16 '22 at 17:31
  • Oops, my assumption was that 'wait' would execute each query in a separate thread, Am I wrong? – user2868835 Sep 16 '22 at 21:27
  • 1
    Yes, thats wrong. "async, await" still *runs* in the same thread. If you really need multiple threads, then have a look at **Isolates**. Please accept and vote up the answer if it helped you. Feel free to ask if you still have questions. – TripleNine Sep 16 '22 at 21:32