[英]setState() causing infinite loop in flutter
so I'm trying to create a loop that will add a string to a List from my Bloc, however whenever i successfully added the string into the list, every single time a setstate is ran the program runs the loop all over again and adds the string into the list, i think adding a boolean for the for loop could work, but is there a more efficient way to do this??所以我试图创建一个循环,将字符串从我的 Bloc 添加到列表中,但是每当我成功地将字符串添加到列表中时,每次运行 setstate 时,程序都会重新运行循环并添加字符串到列表中,我认为为 for 循环添加 boolean 可以工作,但有更有效的方法吗? I only want the loop to run once, even when the build method is called again.我只希望循环运行一次,即使再次调用 build 方法。
code:代码:
Container(
height: MediaQuery.of(context).size.height / 1.2,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => setState(() {}),
child: Text("set state")),
ElevatedButton(
onPressed: () {
BlocProvider.of<BlackjackkBloc>(context).add(
DrawCardEvent(deck_id: deckId!, draw_count: "1"));
setState(() {
dealerCards++;
dealerVal += 10;
});
},
child: Text("deal")),
BlocBuilder<BlackjackkBloc, BlackjackkState>(
builder: (context, state) {
if (state is BlackjackkDrawLoaded) {
for (int i = 0; i < state.bjdraw.cards.length; i++) {
print("going $i");
cards.add(state.bjdraw.cards[i].image);
print("repeat");
}
return Column(
children: List.from(cards.map((name) => Text(name))),
);
/*return Text("${cards}");*/
}
return Text("o");
}),
Text("cards length = ${cards.length}"),
Container(
height: 150,
width: 100 * dealerCards.toDouble(),
child: ListView.builder(
physics: BouncingScrollPhysics(),
/*separatorBuilder: (context, index) => SizedBox(
width: 0.1,
),*/
itemCount: dealerCards,
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext context, int index) {
return Container(
width: 100,
height: 150,
child: CachedNetworkImage(
imageUrl:
"https://deckofcardsapi.com/static/img/AS.png",
),
);
},
),
),
SizedBox(height: 30),
SizedBox(
height: 40,
),
Text(
"Dealer",
style: desctext,
),
SizedBox(height: 10),
Text(playerCards.toString()),
Text(
dealerVal.toString(),
style: cardtext,
),
BlocBuilder<BlackjackkBloc, BlackjackkState>(
builder: (context, state) {
if (state is BlackjackkShuffleLoaded) {
deckId = state.bjshuffle.deckId;
return Text(state.bjshuffle.deckId);
}
if (state is BlackjackkLoad) {
return CircularProgressIndicator();
}
return Text("blum load");
},
)
],
),
),
Try add a buildWhen method in your blocBuilder that only builds if current state differ from previous state.尝试在您的 blocBuilder 中添加一个 buildWhen 方法,该方法仅在当前 state 与以前的 state 不同时构建。
BuildWhen: (previous, current) { current.state != previous.state }
Why are using setState within the bloc?为什么在集团内使用 setState?
Emit a new state from the DrawCardEvent function and change your UI as per that state.从 DrawCardEvent function 发出一个新的 state 并按照 state 更改您的 UI。
I guess you are using a bloc instead of a cubit.我猜你使用的是块而不是肘。 If you are new to the bloc, maybe start with cubit.如果您是该集团的新手,也许从肘开始。 Cubit is simple and is good enough in 99% of the business cases. Cubit 很简单,在 99% 的业务案例中都足够好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.