I'm facing an issue trying to use FutureBuilder with Provider and cache. The computation is done one time and then its cached. The code looks like this:
FutureBuilder(
future: model.calculateStats(chartType: getChartType()),
builder: (context, snapshot) {
if(snapshot.connectionState != ConnectionState.done) {
return Center(
child: CircularProgressIndicator()
);
}
return buildChart(model);
},
)
That piece of code is inside a Consumer with a ViewModel, which has the method calculateStats
, which is the following:
Future calculateStats({ChartType chartType = ChartType.Monthly}) async {
return await MemoryCache.instance.getOrCreateAsync("stats:${chartType.index}", () async {
var statsMaker = StatsMaker(chartType: chartType);
this._currentStats = await statsMaker.generate(
startDate: _getStartDateForChartType(chartType),
endDate: DateTime.now()
);
}, allowNull: true);
}
Here you can see a video of what is happening: https://i.imgur.com/SWQ7N7P.mp4
The helper class MemoryCache checks if the provided key is in a map, and if that is true, it returns the value without doing any computation, which should return immediately, if it not found, the future is awaited and the result stored. But here, although the result is cached, the FutureBuilder shows the loading indicator (which is orange in the video). What am I doing wrong?