如何解决Flutter:如何修复 FutureBuilder 中由 setState 引起的错误
我的目标是在单击 ExpansionTile 时加载一次 Future 并存储值,以便在 Tile 关闭然后重新打开时不会再次加载 Future。
我当前代码的问题是这个错误:
uint64_t minimumPasses(uint64_t m,uint64_t w,uint64_t p,uint64_t n) {
if ((n / m) <= w) {
return 1;
}
uint64_t currentProductionStorage = 0;
uint64_t minRounds = std::numeric_limits<uint64_t>().max();
uint64_t currentRounds = 0;
uint64_t currentProductionSpeed;
while (true) {
currentRounds++;
currentProductionSpeed = m * w;
currentProductionStorage += currentProductionSpeed;
uint64_t roundsWithoutFurtherInvest = 0;
if (n >= currentProductionStorage) {
roundsWithoutFurtherInvest = (n + currentProductionSpeed - 1 - currentProductionStorage) / (currentProductionSpeed);
}
if (minRounds < currentRounds + roundsWithoutFurtherInvest) {
break;
}
minRounds = currentRounds + roundsWithoutFurtherInvest;
if (currentProductionStorage < p) {
uint64_t roundsBeforeInvest = (p + currentProductionSpeed - 1 - currentProductionStorage) / currentProductionSpeed;
currentProductionStorage += (roundsBeforeInvest * m * w);
currentRounds += roundsBeforeInvest;
}
uint64_t numOfPurchasedUnits = currentProductionStorage / p;
currentProductionStorage %= p;
uint64_t diff = std::llabs(w - m);
uint64_t part1 = numOfPurchasedUnits;
uint64_t part2 = 0;
if (diff < numOfPurchasedUnits) {
uint64_t remainingPurchasedUnits = numOfPurchasedUnits - diff;
part1 = diff + (remainingPurchasedUnits / 2);
part2 = (remainingPurchasedUnits / 2) + (remainingPurchasedUnits % 2);
}
if (m > w) {
w += part1;
m += part2;
} else {
m += part1;
w += part2;
}
}
return minRounds;
}
我知道错误是关于什么的,但我不知道如何修复它。这是我当前的代码:
I/flutter (30528): The following assertion was thrown building FutureBuilder<Trim>(dirty,state:
I/flutter (30528): _FutureBuilderState<Trim>#90a6d):
I/flutter (30528): setState() or markNeedsBuild() called during build.
I/flutter (30528): This Dashboard widget cannot be marked as needing to build because the framework is already in the
I/flutter (30528): process of building widgets. A widget can be marked as needing to be built during the build phase
I/flutter (30528): only if one of its ancestors is currently building. This exception is allowed because the framework
I/flutter (30528): builds parent widgets before children,which means a dirty descendant will always be built.
I/flutter (30528): Otherwise,the framework might not visit this widget during this build phase.
I/flutter (30528): The widget on which setState() or markNeedsBuild() was called was:
I/flutter (30528): Dashboard
I/flutter (30528): The widget which was currently being built when the offending call was made was:
I/flutter (30528): FutureBuilder<Trim>
解决方法
您以后不能使用 setstate 或流构建器只需删除 setstate
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。