I am using Flutter Future Builder with ListView which display data loaded from an API.
When the Future Builder is in ConnectionState.waiting
the app gets Stuck.
FutureBuilder<List<int>>(
future: totals,
builder: (BuildContext context, AsyncSnapshot<List<int>> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none: return new Text('Press button to start');
case ConnectionState.active:
case ConnectionState.waiting: return new Text('Awaiting result...');
case ConnectionState.done:
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
else
return RefreshIndicator(
onRefresh: _refresh,
child: ListView(
children: [
Column(
children: [
Text("Total Confirmed : ${snapshot.data[0]}"),
Text("Total Active : ${snapshot.data[1]}"),
Text("Today Confirmed : ${snapshot.data[2]}"),
],
),
],
)
);
break;
default:
return null;
}
},
)
Future<List<int>> totals;
void initState() {
super.initState();
totals=_fetchtotals();
}
Future<void> _refresh() async{
totals=_fetchtotals();
}
Future<List<int>> _fetchtotals() async {
//loading data from API and returning
return totals;
}