Background I have a class PriceScreen that contains an asynchronous method getData(). This PriceScreen class makes a call to class Graph using data from graphValues returned from getData().
Problem
The call to Graph Graph(closingTimesAndPrices: graphValues)
is running before getData is completed, resulting in the passed graphValues becoming Null in the call above. How can I change my code so that the call to Graph waits to be called until getData is finished processing?
Note: I have added "<<<----------" to parts of the code that I believe are important to answering my question.
class PriceScreen extends StatefulWidget {
@override
PriceScreenState createState() => PriceScreenState();
}
class PriceScreenState extends State < PriceScreen > {
String selectedCurrency = 'USD';
String selectedGraphType = "1M";
Map < String,
String > coinValues = {};
Map < String,
double > graphValues = {};
bool isWaiting = false;
void getData() async {<<< -- -- -- -- -- -- -- -- -- -
isWaiting = true;
try {
Map coinData = await CoinData().getCoinData(selectedCurrency);
Map graphData = await GraphData().getGraphData( <<< -- -- -- -- -
selectedCurrency: selectedCurrency,
selectedGraphType: selectedGraphType);
isWaiting = false;
setState(() {
coinValues = coinData;
graphValues = graphData; <<< -- -- -- -- -- -
});
} catch (e) {
print(e);
}
}
@override
void initState() {
super.initState();
getData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Crypto Watcher'),
),
body: ModalProgressHUD(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: < Widget > [
Graph(closingTimesAndPrices: graphValues), <<< -- -- --
]),
inAsyncCall: isWaiting,
progressIndicator: CircularProgressIndicator(
backgroundColor: Colors.orange,
),
),
);
}
}