有没有办法知道所有小部件(包括 FutureBuilder 小部件)何时在 Flutter 中构建。 WidgetsBinding.instance.addPostFrameCallback
在构建完成时、FutureBuilder 构建之前调用。
我在 FutureBuilder 中有一个列表,其数据是从服务器获取的。FutureBuilder 之外还有另一个 Text 小部件,它显示列表项的数量。我需要在填充列表时更新此计数小部件。如果不使用流,则在构建 FutureBuilder 时是否会触发回调方法。
小智 7
WidgetsBinding.instance.addPostFrameCallback
当有数据和连接完成时,只需在您的 futurebuilder 中使用
FutureBuilder<List<Object>>(
future: _listFuture,
builder: (context, AsyncSnapshot snapshot) {
if(snapshot.hasData && snapshot.connectionState == ConnectionState.done){
WidgetsBinding.instance
.addPostFrameCallback((_) {
});
}
return ...
}
);
Run Code Online (Sandbox Code Playgroud)
有很多方法。如果您打算在不使用状态管理的情况下执行此操作,则可以使用 ValueNotifier。
ValueNotifier<int> total = ValueNotifier(0);
FutureBuilder()..future.then((snapshot) {
total.value = snapshot.total;
});
ValueListenableBuilder(
valueListenable: total,
builder: (context, value, widget) {
return Text(value.toString());
},
),
Run Code Online (Sandbox Code Playgroud)
我建议你使用提供商包
归档时间: |
|
查看次数: |
2956 次 |
最近记录: |