gpt4 book ai didi

flutter - Statefulwidget不刷新ListView

转载 作者:行者123 更新时间:2023-12-03 04:34:36 26 4
gpt4 key购买 nike

我将从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),
),
);
}
}

最佳答案

尝试在调用await之前添加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();
});
},
)

关于flutter - Statefulwidget不刷新ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64332192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com