当FutureBuilder
完成加载时,我正在试图找出一种方法来向周围的班级表明。 RefreshIndicator
将Future
作为参数,并在Future
完成时停止显示刷新指示器。我没有访问传递给Future
的完全相同的FutureBuilder
变量,特别是当它们在两个单独的类中时,除非我可以将引用传递给一个,当它在另一个类中完成时,我会知道。 ..
我也在寻找这个答案。最后我想通了......
这是我的表现
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: () {
return _calculation= getCalculation(); // EDITED
},
child: SingleChildScrollView(
SingleChildScrollView
physics: AlwaysScrollableScrollPhysics(),
child: Text('Result: ${snapshot.data}')
)
);
break;
default:
return null;
}
},
)
Future<String> getCalculation() async {
/*Write your API here or what ever u want to get when pull to refresh*/
return null;
}
你必须访问快照,提供builder
parm:
因此,snapshot.data为您提供了Future。
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/
Doc:https://docs.flutter.io/flutter/widgets/FutureBuilder-class.html