使用FutureBuilder时重新加载数据 [英] Reload data when using FutureBuilder
本文介绍了使用FutureBuilder时重新加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在加载小部件时正在加载数据,如下面的代码.完全加载UI后,我想添加一个刷新按钮以再次重新加载数据.
I am loading data when widget is loading like the code below. Once the UI is fully loaded, I like to add one refresh button to reload the data again.
如何刷新视图?
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
var futureBuilder = new FutureBuilder(
future: _getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return new Text('loading...');
default:
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
else
return createListView(context, snapshot);
}
},
);
return new Scaffold(
appBar: new AppBar(
title: new Text("Home Page"),
),
body: futureBuilder,
);
}
Future<List<String>> _getData() async {
var values = new List<String>();
await new Future.delayed(new Duration(seconds: 5));
return values;
}
Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
}
}
推荐答案
Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
RaisedButton button = RaisedButton(onPressed: () {
setState(() {});
}, child: Text('Refresh'),);
//.. here create widget with snapshot data and with necessary button
}
这篇关于使用FutureBuilder时重新加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文