Statefulwidget is not refreshing ListView

67 views Asked by At

I'm saving the data that is fetched from an API to the sqflite in flutter project, everything is working good, except that after clicking a raised button the data should be insert into the table and a new page should be open but there is no data unless I refresh that page so the data appear

As you can see, here is the code of the raised button:

 RaisedButton(
              child: Text('Get Cities'),
              onPressed: () async {
                setState(() {
                 GetAllData.data.Getdata();
               });
                await Navigator.push(context, MaterialPageRoute<void>(
                    builder: (BuildContext context) => StoreList()));
                setState(() {});
              },
            )

Inside the setState I'm calling a function Getdata to get the data from the sqflite, after it getting it the app should open a new page

And below is the code of the page which should show the data in a ListView:

class StoreList extends StatefulWidget { @override


 _StoreListState createState() => _StoreListState();}
class _StoreListState extends State<StoreList> {
@override void initState() {
super.initState();
setState(() {
  DatabaseProvider_API.db.getRoutes();
});}



@override


Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text('Stores List'),
  ),
 body: FutureBuilder<List<Stores>>(
    future: DatabaseProvider_API.db.getStores(),
    builder: (context, snapshot){
      if(snapshot.data == null){
        return Center(
          child: CircularProgressIndicator(),

        );
      }
 else {
        return ListView.separated(
          separatorBuilder: (BuildContext context, int index){
            return Divider();
          },
itemCount: snapshot.data.length,
          itemBuilder: (BuildContext context, int index){
            String name = snapshot.data[index].sTORENAME;
            String name_ar = snapshot.data[index].cITY;
            return ListTile(
              title: Text(name),
              subtitle:Text (name_ar),
              onTap: ()async{
                setState(() {
                });

                await
                Navigator.push(context, MaterialPageRoute<void>(
                    builder: (BuildContext context) => Category() ));
              },
            );
          },
        );
      }
    },
  ),
  floatingActionButton: new FloatingActionButton(
    onPressed: () {
      setState(() {});
    },
    child: new Icon(Icons.update),
  ),
);

} }

1

There are 1 answers

1
Omer Gamliel On BEST ANSWER

Try to add the await keyword before evoke GetAllData.data.GetData()

              RaisedButton(
              child: Text('Get Cities'),
              onPressed: () async {             
                  // await for new data to be inserted
                await GetAllData.data.Getdata();
                await Navigator.push(context, MaterialPageRoute<void>(
                    builder: (BuildContext context) => StoreList()));
                  setState(() {
                    dataFuture =  GetAllData.data.Getdata();
                  });
              },
            )