Calling an async function on FutureBuilder inside onTap returning the same value. (Flutter)

Viewed 367

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?

1 Answers

put counter = value inside setState function. But beware as it will refresh the page too as you are using a future builder.

A more effective way of achieving this is to load the data by calling a function in initState, while showing showing a loading screen I recommend to watch this video https://www.youtube.com/watch?v=nLlVANBmFJM&t=92s for an idea on that. After u do that u can store the counter in a variable and then decrease it per click using setState.

Related