我正在尝试找出一种方法来指示FutureBuilder
完成加载时向周围的类指示。 RefreshIndicator
将Future
作为参数,并在Future
完成时停止显示刷新指示符。我无权访问传递给Future
的完全相同的FutureBuilder
变量,尤其是当它们在两个单独的类中时,除非我可以传递对一个的引用以及它在其他课程,我会知道...
答案 0 :(得分:2)
我也在寻找这个答案。终于我明白了...
这是我的工作方式
FutureBuilder<String>(
future: _calculation, // a previously-obtained Future<String> or null
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Press button to start.');
case ConnectionState.active:
case ConnectionState.waiting:
return Text('Awaiting result...');
case ConnectionState.done:
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
return new RefreshIndicator(
key: _refreshIndicatorKey,
color: Colors.blue,
onRefresh: () {
setState(() {
_calculation= getCalculation();
});
},
child: SingleChildScrollView( // if u are using Listview remove SingleChildScrollView
physics: AlwaysScrollableScrollPhysics(),
child: Text('Result: ${snapshot.data}')
)
);
}
return null; // unreachable
},
) //FutureBuilder
Future<String> getCalculation() async {
/*Write your API here or what ever u want to get when pull to refresh*/
return null;
}
答案 1 :(得分:0)
您必须访问快照,前提是builder
参数:
因此,snapshot.data为您带来未来。
new FutureBuilder<String>(
future: _calculation, // a Future<String> or null
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none: return new Text('Press button to start');
case ConnectionState.waiting: return new Text('Awaiting result...');
default:
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
else
return new Text('Result: ${snapshot.data}');
}
},
)
示例:https://flutter.io/cookbook/networking/background-parsing/
文档:https://docs.flutter.io/flutter/widgets/FutureBuilder-class.html