Flutter 加载数据时更改FutureBuilder中的状态
如果我需要等待Flutter 加载数据时更改FutureBuilder中的状态,flutter,flutter-layout,Flutter,Flutter Layout,如果我需要等待FutureBuilder,如何更新UI?我是否需要调用我未来的函数两次,一次用于构建器,另一次用于更改UI FutureBuilder<String>( future: getUserOrder(4045), builder: (context, snapshot) { if (snapshot.hasData) { return Text(snapshot.data,style: Theme
FutureBuilder
,如何更新UI?我是否需要调用我未来的函数两次,一次用于构建器,另一次用于更改UI
FutureBuilder<String>(
future: getUserOrder(4045),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data,style: Theme.of(context).textTheme.headline);
} else if (snapshot.hasError) {
// I need to change the state at this point
return Text("${snapshot.error}",style: Theme.of(context).textTheme.headline);
} else {
return CircularProgressIndicator();
}
}),
FutureBuilder(
未来:getUserOrder(4045),
生成器:(上下文,快照){
if(snapshot.hasData){
返回文本(snapshot.data,样式:Theme.of(context.textTheme.headline);
}else if(snapshot.hasrerror){
//我现在需要改变状态
返回文本(${snapshot.error}),样式:Theme.of(context.textTheme.headline);
}否则{
返回循环ProgressIndicator();
}
}),
在FutureBuilder
中调用setState
会引发以下错误:
在生成过程中调用setState()或markNeedsBuild()
我不需要显示按钮或任何其他要单击的按钮。我想在将日期加载到
futureBuilder
中时自动执行该操作,因为我无法在futureBuilder
中调用setState。解决方案是将其删除,然后执行如下操作:
getBillingInfo() {
Provider.of<MyRents>(context, listen: false)
.getBillingInfo(context)
.then((billingInfo) {
setState(() {
if (billingInfo["companyInfo"] != null &&
billingInfo["taxes"].isNotEmpty) {
_canGenerateInvoices = true;
} else {
_canGenerateInvoices = false;
}
});
});
}
这样,当我执行其他操作时,我总是可以更改
\u canGenerateInvoices
的值。您的意思是如果发生错误,如何使用状态重试未来?这是否回答了您的问题?您可以调用setState()
。但目标是什么?您不能在futureBuilder(稍后我会添加错误)中调用setState更新问题
void initState() {
super.initState();
getBillingInfo();
}
Visibility(
visible: _canGenerateInvoices,
child: MyWidget()
)