众所周知 在flutter中有两种状态的widget :
/// Creates a widget that builds itself based on the latest snapshot of
/// interaction with a [Future].
///
/// The [builder] must not be null.
我们来试验一下如下代码:
FutureBuilder(
future: Future.delayed(
Duration(seconds: 2), () => 100), //throw('error msg')
initialData: 10,
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
print(snapshot.connectionState);
if (snapshot.hasError) {
return Text('error');
}
return Text(
'${snapshot.data}',
style: TextStyle(fontSize: 30),
);
})
控制台输出
即当前的snapshot.connectionState 状态有两种 等待状态 和 执行完毕状态
可以看到 由初始值10 变成了我们的最终值100
当然error也是可以的
以下代码
FutureBuilder(
future: Future.delayed(Duration(seconds: 2),
() => throw ('error msg')), //throw('error msg')
initialData: 10,
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
print(snapshot.connectionState);
if (snapshot.hasError) {
return Text('error',
style: TextStyle(fontSize: 30, color: Colors.red));
}
return Text('${snapshot.data}', style: TextStyle(fontSize: 30));
})