衆所周知 在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));
})