很多时候我们有这样一个需求,需要在网络请求完了之后再去渲染页面,尤其是在一个界面有多个相关网络请求的时候需要处理。这里不得不提到一个系统的组件FutureBuilder,我们直接来看用法。
单网络请求时:
@override
Widget buildWidget(BuildContext context, Store<UserState> store) {
// TODO: implement buildWidget
return Scaffold(
body: FutureBuilder(
future: fetchPost,
builder: (BuildContext context, AsyncSnapshot snapShot) {
print('connectionState:${snapShot.connectionState}');
if (snapShot.connectionState == ConnectionState.waiting) {
return Text('Loading...');
} else if (snapShot.connectionState == ConnectionState.done) {
print(snapShot.hasError);
print('data:${snapShot.data}');
if (snapShot.hasError) {
return Text('Error: ${snapShot.error}');
}
return ListView(