【发布时间】:2021-05-18 11:35:58
【问题描述】:
Riverpod 使用 .whenData() 提供了一种速记方式,您无需提供加载和错误参数。但我找不到如何使用此代码在 build() 函数中返回小部件的示例。
Widget build(BuildContext context, ScopedReader watch) {
final cityListFuture = watch(cityListFutureProvider);
// This one is working fine
return cityListFuture.when(
data: (value) {
return Text("Data goes here");
},
loading: () => CircularProgressIndicator(),
error: (error, stack) {
return Container();
});
// This is shorthand for .when() without the need of loading and error
// ERROR: The return type 'AsyncValue<Text>' isn't a 'Widget', as required by the closure's context.
return cityListFuture.whenData((value) => Text("Data goes here"));
}
有人知道我们如何使用 .whenData() 来返回一个小部件吗?
【问题讨论】: