FutureBuilder
1 2 3 4 5 6 7 8 9 10 11
| FutureBuilder({ this.future, this.initialData, required this.builder, })
final AsyncWidgetBuilder<T> builder;
typedef AsyncWidgetBuilder<T> = Widget Function(BuildContext context, AsyncSnapshot<T> snapshot);
|
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
Widget build(BuildContext context) { return Center( child: FutureBuilder<String>( future: mockNetworkData(), builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done) { if (snapshot.hasError) { return Text("Error: ${snapshot.error}"); } else { return Text("Contents: ${snapshot.data}"); } } else { return CircularProgressIndicator(); } }, ), ); }
enum ConnectionState { none,
waiting,
active,
done, }
|
StreamBuilder
Dart中,Stream也是用于异步事件数据,和Future不同的是,它可以接收多个异步操作的结果。
StreamBuilder正式用于配合Stream来展示流上事件(数据)变化的UI组件。
1 2 3 4 5
| StreamBuilder({ this.initialData, Stream<T> stream, required this.builder, })
|
参考