有了这些数据,我正在尝试制作饼图 class Stats extends StatefulWidget { @override _Sta-6ren">
gpt4 book ai didi

flutter - 如何解决此错误 "setState() or markNeedsBuild() called during build."?

转载 作者:行者123 更新时间:2023-12-03 04:46:54 26 4
gpt4 key购买 nike

所以我在沼地有一张 table ,它返回 Future<List<..>>有了这些数据,我正在尝试制作饼图


class Stats extends StatefulWidget {

@override
_StatsState createState() => _StatsState();
}

class _StatsState extends State<Stats> {



@override
void initState() {
super.initState();


}

@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_buildTaskList(context)

],
);
}
}

FutureBuilder<List<Account>> _buildTaskList(BuildContext context) {


Map<String, double> dataMap = Map();
dataMap.putIfAbsent("SNACKS", () => Provider.of<Amount>(context,listen: false).snack_amount);
dataMap.putIfAbsent("ENTERTAINMENT", () => Provider.of<Amount>(context,listen: false).entertainment_amount);
dataMap.putIfAbsent("STATIONARY", () => Provider.of<Amount>(context,listen: false).stationary_amount);
dataMap.putIfAbsent("OTHERS", () => Provider.of<Amount>(context,listen: false).others_amount);

final dao = Provider.of<AccountDao>(context);
return FutureBuilder(
future: dao.getAllAccounts(),
builder: (context, AsyncSnapshot<List<Account>> snapshot) {
final accounts = snapshot.data ?? List();

Provider.of<Amount>(context,listen: false).add(Calculate(accounts, Type.SNACKS), Calculate(accounts, Type.ENTERTAINMENT),
Calculate(accounts, Type.STATIONARY), Calculate(accounts, Type.OTHERS));



if (accounts == null) {
return Text('No Accounts Yet');
} else {
return PieChart(
dataMap: dataMap,
);
}
},
);
}

double Calculate(List<Account> list, Type type){

double sum=0;
for(int i=0;i<list.length;i++){
if(list[i].type==type){
sum=sum+list[i].amount;
}
}

return sum;
}


现在每次我运行这个我都会收到一个错误

**在为 Amount 发送通知时抛出以下断言:
在构建期间调用 setState() 或 markNeedsBuild()。

这个 _InheritedProviderScope 小部件不能被标记为需要构建,因为框架已经在构建小部件的过程中。仅当其祖先之一当前正在构建时,小部件才能在构建阶段标记为需要构建。这个异常(exception)是允许的,因为框架在子组件之前构建父小部件,这意味着将始终构建脏后代。否则,框架可能不会在此构建阶段访问此小部件。
调用 setState() 或 markNeedsBuild() 的小部件是:_InheritedProviderScope
值:“金额”的实例
聆听值(value)
发出违规调用时当前正在构建的小部件是:FutureBuilder>
肮脏的
状态:_FutureBuilderState>#37282
抛出异常时,这是堆栈:

0 元素.markNeedsBuild。 (包:flutter/src/widgets/framework.dart:4167:11)

1 Element.markNeedsBuild(包:flutter/src/widgets/framework.dart:4182:6)

2 _InheritedProviderScopeElement.markNeedsNotifyDependents (包:provider/src/inherited_provider.dart:426:5)

3 ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:207:21)

4 Amount.add (package:moneymanager/main.dart:55:5)

...
金额发送通知是:“金额”的实例

**

这是 金额类
class Amount extends ChangeNotifier {

double snack_amount=0.0;
double entertainment_amount=0.0;
double stationary_amount=0.0;
double others_amount=0.0;

void add(double snack,double entertainment,double stationary, double others){
snack_amount=snack;
entertainment_amount=entertainment;
stationary_amount=stationary;
others_amount=others;
notifyListeners();
}
}


获取 Future<List<..>> 的函数

``` future > getAllAccounts()=>select(accounts).get();``

编辑
我按照建议编辑了我的答案
return FutureBuilder(
future: dao.getAllAccounts(),
builder: (context, AsyncSnapshot<List<Account>> snapshot) {

if(snapshot.connectionState==ConnectionState.done){

final accounts = snapshot.data ?? List();




if (accounts == null) {

return Text('No Accounts Yet');
} else {
Provider.of<Amount>(context,listen: false).add(Calculate(accounts, Type.SNACKS), Calculate(accounts, Type.ENTERTAINMENT),
Calculate(accounts, Type.STATIONARY), Calculate(accounts, Type.OTHERS));

dataMap.putIfAbsent("SNACKS", () => Provider.of<Amount>(context,listen: false).snack_amount);
dataMap.putIfAbsent("ENTERTAINMENT", () => Provider.of<Amount>(context,listen: false).entertainment_amount);
dataMap.putIfAbsent("STATIONARY", () => Provider.of<Amount>(context,listen: false).stationary_amount);
dataMap.putIfAbsent("OTHERS", () => Provider.of<Amount>(context,listen: false).others_amount);


return PieChart(
dataMap: dataMap,
);
}

}else if(snapshot.connectionState==ConnectionState.waiting){

return Container();
}else{
return Container();
}


},


但仍然是同样的错误

最佳答案

The following assertion was thrown while dispatching notifications for Amount: setState() or markNeedsBuild() called during build.



此错误表示您在构建阶段调用 setState

并从您的日志中明确指出

仅当其祖先之一当前正在构建时,小部件才能在构建阶段标记为需要构建。这个异常(exception)是允许的,因为框架在子组件之前构建父窗口小部件,这意味着将始终构建脏后代。否则,框架可能不会在此构建阶段访问此小部件

你可以实现 ConnectionState.waiting ConnectionState.done

关于flutter - 如何解决此错误 "setState() or markNeedsBuild() called during build."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62127703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com