I have a FutureBuilder that loads data from sqflite and builds horizontal listview.
FutureBuilder<List<Favorite>>(
future: DatabaseHelper.instance.retrieveFavorite(),
builder: (BuildContext context,
AsyncSnapshot<List<Favorite>> snapshot){
return snapshot.hasdata?Row(
//data from sqflite
children: <Widget>[
Expanded(
child: SizedBox(
height: 200.0,
child: ListView.separated(
addAutomaticKeepAlives: false,
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.length,
itemBuilder:
(BuildContext context,
int index) {
return (index < 6)
? InkWell(
onTap: () {
load().then((value) {
counter=value;
});
print('$counter');
if (counter >= 100) {
ModelChannel t =
convertSingle(
snapshot.data[
index]);
Navigator.push(
context,
MaterialPageRoute(
builder:
(context) =>
LiveTvPlayer(
channel: t,
),
),
);
}
}
the load function looks like this-
Future<int> load() async {
final SharedPreferences prefs = await _prf;
int i = (prefs.getInt("Rcounter") ?? 500);
return i;
}
Everytime someone clicks any list item the counter decreses by 100. But the load function returns the same value. It only returns different value if the app is restarted. And the function works perfectly if it it is called from any other place. Why it is happening inside futurebuilder and how to solve this?