Statefulwidget 不刷新 ListView
Statefulwidget is not refreshing ListView
我正在将从 API 获取的数据保存到 flutter 项目中的 sqflite,一切正常,除了在单击凸起的按钮后数据应该插入到 table 应该会打开一个新页面,但没有数据,除非我刷新该页面以便显示数据
如您所见,这是凸起按钮的代码:
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();
});
},
)
我正在将从 API 获取的数据保存到 flutter 项目中的 sqflite,一切正常,除了在单击凸起的按钮后数据应该插入到 table 应该会打开一个新页面,但没有数据,除非我刷新该页面以便显示数据
如您所见,这是凸起按钮的代码:
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();
});
},
)