Statefulwidget is not refreshing ListView



我正在保存从API提取到flutter项目中的sqflite的数据,一切都很好,只是在点击一个凸起的按钮后,数据应该被插入到表中,并且应该打开一个新页面,但没有数据,除非我刷新该页面,使数据显示为

正如你所看到的,这是升起按钮的代码:

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

在setState中,我调用了一个函数Getdata来从sqflite中获取数据,在获取数据后,应用程序应该打开一个新的页面

下面是应该在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),
),
);

}}

在调用GetAllData.data.GetData()之前尝试添加await关键字

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();
});
},
)

最新更新