向上滑动刷新flutter FUTUREBUILDER



我有一个应用程序,它从firebase获取一些数据,然后调用一个类来显示基于firebase数据的小部件。我试着添加一个向上滑动刷新,但我不知道把它放在哪里,以及在刷新时要调用什么。我正在尝试使用RefreshIndicator。

这里我将把我的代码,它调用数据库(firebase),然后创建一个小部件在数据库中的每个事件。

如果你需要更多的信息,请随时评论。非常感谢你的帮助!

FutureBuilder(
future: databaseReference.once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
List lists = [];
if (snapshot.hasData) {
lists.clear();
Map<dynamic, dynamic> values = snapshot.data.value;
values.forEach((key, values) {
lists.add(values);
});
return new ListView.builder(
primary: false,
padding: EdgeInsets.only(left:12.0,right:12,bottom: 15,),
shrinkWrap: true,
itemCount: lists.length,
itemBuilder: (BuildContext context, int index) {
if(lists[index]["Status"]== "Active"){;
return Container(
child:EvendWidget(lists[index]["EventImage"], 
Text(lists[index]["EventName"]).data,
Text(lists[index]["EventLocation"]+ ", "+lists[index] 
["EventCounty"] ).data,
Text(lists[index]["Date"]+ ", "+lists[index]["Time"]+ 
" - "+lists[index]["Time"]).data,
Text(lists[index]["Duration"]+ " H").data,
Text(lists[index]["Genre"]).data,
Text(lists[index]["Price"]).data,false));}else{return 
SizedBox.shrink(); };
});
}
return Container(
margin: const EdgeInsets.only(top: 300),
child:CircularProgressIndicator());
}),

如果你需要更多的信息,请随时评论。非常感谢你的帮助!

在滑动动作中调用setState。触发控件中的build方法,再次调用您的future

onRefresh: () {
setState(() {});
}
RefreshIndicator(
onRefresh: () {
return Future(() { setState(() {}); });
},
child: FutureBuilder(
...

将帮助你

您可以使用RefreshIndicator为滑动刷新。https://api.flutter.dev/flutter/material/RefreshIndicator-class.html

使用列表视图。Builder作为RefreshIndicator的子元素,在onRfresh中使用你的future方法

:

//define in widget
var refreshkey = GlobalKey<RefreshIndicatorState>();
FutureBuilder(
future: databaseReference.once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
List lists = [];
if (snapshot.hasData) {
lists.clear();
Map<dynamic, dynamic> values = snapshot.data.value;
values.forEach((key, values) {
lists.add(values);
});
return RefreshIndicator(
key: refreshkey,
onRefresh: databaseReference.once(),
child: ListView.builder(
primary: false,
padding: EdgeInsets.only(left:12.0,right:12,bottom: 15,),
shrinkWrap: true,
itemCount: lists.length,
itemBuilder: (BuildContext context, int index) {
if(lists[index]["Status"]== "Active"){;
return Container(
child:EvendWidget(lists[index]["EventImage"], 
Text(lists[index]["EventName"]).data,
Text(lists[index]["EventLocation"]+ ", "+lists[index] 
["EventCounty"] ).data,
Text(lists[index]["Date"]+ ", "+lists[index]["Time"]+ 
" - "+lists[index]["Time"]).data,
Text(lists[index]["Duration"]+ " H").data,
Text(lists[index]["Genre"]).data,
Text(lists[index]["Price"]).data,false));}else{return 
SizedBox.shrink(); };
});
)

}
return Container(
margin: const EdgeInsets.only(top: 300),
child:CircularProgressIndicator());
}),

可能需要一些编辑

最新更新