How can I let Flutter know to wait for one function (and therefore the future) to complete, until the next function is called? It seems like the load() function itself completes before the loadFeed() Future is finished, how can I fix that?
Here is my current code:
ValueNotifier<List> data = ValueNotifier([]);
ValueNotifier<bool> loading = ValueNotifier(true);
loadAll() async {
await load(0, 'url1');
print("0 finished");
await load(1, 'url2');
print("1 finished");
await load(2, 'url3');
print("2 finished");
}
load(int categoryIndex, String _url) async {
loadFeed(_url).then((result) {
if (null == result || result.toString().isEmpty) {
print("error");
return;
}
data.value = [[],[],[],];
for (var i = 0; i < result.items.length; i++) {
data.value[categoryIndex].add(result.items[i]);
}
data.notifyListeners();
});
}
Future<RssFeed> loadFeed(String _url) async {
loading.value = true;
try {
final client = http.Client();
final response = await client.get(Uri.parse(_url));
return RssFeed.parse(response.body);
} catch (e) {
print("error");
} finally {
loading.value = false;
}
return null;
}