I'm still trying to understand how future works in dart, I wrote a method, that fetches data from an API like so:
Future<CountryData> getCountryData(String country) async {
CountryData data;
await apiService.getCountryData(country).then((country) {
data = country;
});
return data;
}
But when calling the method it does not wait for execution to complete. This is how the method is being called:
Future<void> _updateData() async {
try {
final dataRepository =
Provider.of<DataRepository>(context, listen: false);
**final endpointcountrydata = await dataRepository.getCountryData("nigeria");**
final africaData = await dataRepository.getAfricaData();
setState(() {
_endpointsData = endpointsData;
_countryData = endpointcountrydata;
_africaModel = africaData;
});
} on SocketException catch (_) {
showAlertDialog(
context: context,
title: 'Connection Error',
content: 'Could not retrieve data. Please try again later.',
defaultActionText: 'OK',
);
} catch (_) {
showAlertDialog(
context: context,
title: 'Unknown Error',
content: 'Please contact support or try again later.',
defaultActionText: 'OK',
);
}
}
Both methods (endpointcountrydata and africaData) don't wait for exceution to complete.
The _updateData() method is called on the initState like so:
@override
void initState() {
super.initState();
final dataRepository = Provider.of<DataRepository>(context, listen: false);
**dataRepository.getAfricaData().then((value){
print(value);
});**
_updateData();
}
I even tried calling the method (getAfricaData()) from initState, still not waiting.
where am I getting it wrong?
From answers below, I have tried this:
Future<CountryData> getCountryData(String country) async {
CountryData data = await apiService.getCountryData(country);
return data;
}
Then I instatiated a bool like so:
bool _isFetchingData;
This is how the _updateData method looks like:
Future<void> _updateData() async {
try {
final dataRepository =
Provider.of<DataRepository>(context, listen: false);
final endpointcountrydata = await dataRepository.getCountryData("nigeria");
final endpointsData = await dataRepository.getAllEndpointsData();
final africaData = await dataRepository.getAfricaData();
setState(() {
_isFetchingData = true;
_endpointsData = endpointsData;
_countryData = endpointcountrydata;
_africaModel = africaData;
});
if(_countryData != null && _africaModel != null){
setState(() {
_isFetchingData = false;
});
}
} on SocketException catch (_) {
showAlertDialog(
context: context,
title: 'Connection Error',
content: 'Could not retrieve data. Please try again later.',
defaultActionText: 'OK',
);
} catch (_) {
showAlertDialog(
context: context,
title: 'Unknown Error',
content: 'Please contact support or try again later.',
defaultActionText: 'OK',
);
}
}
Then Initstate:
@override
void initState() {
super.initState();
final dataRepository = Provider.of<DataRepository>(context, listen: false);
_endpointsData = dataRepository.getAllEndpointsCachedData();
_updateData();
}
Yet, still not waiting. Pulling my hair out already.