Ошибка флаттера: "Виджет не может быть собран, потому что он уже находится в процессе сборки"

Я получил эту ошибку:

I/flutter (29346): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (29346): The following assertion was thrown building MainLogic(dirty, state: _MainLogic#9c794):
I/flutter (29346): setState() or markNeedsBuild() called during build.
I/flutter (29346): This InhWidget widget cannot be marked as needing to build because the framework is already in the
I/flutter (29346): process of building widgets. A widget can be marked as needing to be built during the build phase
I/flutter (29346): only if one of its ancestors is currently building. This exception is allowed because the framework
I/flutter (29346): builds parent widgets before children, which means a dirty descendant will always be built.
I/flutter (29346): Otherwise, the framework might not visit this widget during this build phase.
I/flutter (29346): The widget on which setState() or markNeedsBuild() was called was:
I/flutter (29346):   InhWidget(state: InhState#3aaa8)
I/flutter (29346): The widget which was currently being built when the offending call was made was:
I/flutter (29346):   MainLogic(dirty, state: _MainLogic#9c794)

Прежде чем наполнить вас кодом, я хотел бы немного объяснить логику: я пытаюсь реализовать правила игры баккара, для этого я настраиваю унаследованный виджет, какой-то виджет без состояния отправляет входные данные в сохраненный объект в унаследованном состоянии, согласно данным этого объекта, другой виджет перестраивает себя.

последнее изменение, которое я сделал, это применение "правил рисования" баккары, которые могут заставить Объект пропускать ввод при необходимости, но когда это происходит, унаследованный виджет не любит его.

Я опубликую последнюю часть кода, но если вы считаете, что что-то еще имеет отношение, просто спросите, и я опубликую это.

заранее благодарю за помощь

    class MainLogic extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _MainLogic();
}
class _MainLogic extends State<MainLogic> {
  Widget cardGrid = CardGrid();
  Widget newGame = NewGame();
  Rules rules;
  bool gameOver;
  @override
  void initState(){
    super.initState();
    gameOver = false;
  }
  @override
  Widget build(BuildContext context) {
    final InhState state = InhWidget.of(context);
    rules = new Rules(game:state.game);
    if(gameOver==false && state.game.count>3 && rules.playerExtraCard()==false)
    {state.skip();}
    if(gameOver==false && state.game.count>4 && rules.bankerExtraCard()==false)
    {state.skip();}
    checkGame(state.game.count);
    return Container(child:status(gameOver));
  }
  status(bool _gameOver) {
    Widget whichOne;
    if (_gameOver) {whichOne=newGame;}
    else {whichOne=cardGrid;}
    return whichOne;
  }
  void checkGame(int cards) {
    if (cards >= 6) {
      gameOver = true;
    } else {gameOver = false;}
  }

...

   class Rules {
  final Game game;
  Rules({this.game});
  static Deck deck = new Deck();
  int get p1 => deck.getValue(game.p1);
  int get p2 => deck.getValue(game.p2);
  int get p3 => deck.getValue(game.p3);
  int get b1 => deck.getValue(game.b1);
  int get b2 => deck.getValue(game.b2);
  int get b3 => deck.getValue(game.b3);
  normalize(List<int> input) {
    int normal;
    int initial = 0;
    input.forEach((num e){initial += e;});
    if(initial>9) {normal=initial-10;}
    else {normal=initial;}
    return normal;
  }
  playerExtraCard() {
    bool extraCard;
    int pInit = normalize([p1,p2]);
    int bInit = normalize([b1+b2]);
    if (pInit > 5) {extraCard = false;}
    else if (bInit > 7) {extraCard = false;}
    else {extraCard = true;}
    return extraCard;
  }
  bankerExtraCard() {
    bool extraCard;
    int pInit = normalize([p1,p2]);
    int bInit = normalize([b1+b2]);
    int pWing = normalize([p3]);
    if (pInit>7) {extraCard = false;}
    else if (bInit>6) {extraCard = false;}
    else if (pInit>5 && bInit<6) {extraCard = true;}
    else if (pInit<6 && bInit==6 && pWing<8 && pWing>5) {extraCard = true;}
    else if (pInit<6 && bInit==5 && pWing<8 && pWing>3) {extraCard = true;}
    else if (pInit<6 && bInit==4 && pWing<8 && pWing>1) {extraCard = true;}
    else if (pInit<6 && bInit==3 && pWing==8) {extraCard = true;}
    else if (pInit<6 && bInit<3) {extraCard = true;}
    else {extraCard = false;}
    return extraCard;
  }
}

...

   class InhState extends State<InhWidget> {
  Game game = new Game();
  @override
  void initState() {
    super.initState();
    game.blank();
  }
  void deal(String card) {
    setState(() {
      game.cards[game.count] = card;
      game.count = game.count + 1;
    });
  }
  void restart(){
    setState(() {
      game.blank();
    });
  }
  void skip() {
    setState(() {
      game.count = game.count + 1;
    });
  }
  @override
  Widget build(BuildContext context) {
    return new InhCore(
      data: this,
      child: widget.child,
    );
  }
}

...

   class InhWidget extends StatefulWidget {
  InhWidget({this.child});
  final Widget child;
  @override
  State<StatefulWidget> createState() => InhState();
  static InhState of(BuildContext context) {
    return (context.inheritFromWidgetOfExactType(InhCore) as InhCore).data;
  }
}

2 ответа

Ты звонишь state.skip() в build() а также setState() в skip()

  void skip() {
    setState(() {
      game.count = game.count + 1;
    });
  } 

Обычно это хорошая идея, чтобы избежать "бизнес-логики" в build(),

Не должно быть необходимости вычислять все то, что вы делаете, используя InhState в build(), build() можно вызывать очень часто в разное время.

Вы должны скорее обновить состояние в зависимости от событий взаимодействия с пользователем (onTap:,...) или другие источники событий, такие как потоки или фьючерсы, которые указывают на изменения состояния от асинхронных операций.

Я бы встал на сторону ответа @Günter Zöchbauer и реорганизовал код, как он советует, но для более быстрого исправления вы можете попробовать обернуть state.skip(); инструкции (те, что внутри build()) в WidgetsBinding.instance.addPostFrameCallback. Таким образом, вы отложите операцию изменения состояния сразу после сборки.

      @override
Widget build(BuildContext context) {
  final InhState state = InhWidget.of(context);
  rules = new Rules(game:state.game);
  if (!gameOver) {
    if (state.game.count > 3 && !rules.playerExtraCard() ||
        state.game.count > 4 && !rules.bankerExtraCard())
    {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        state.skip();
      });
    }
  }

  checkGame(state.game.count);
  return Container(child:status(gameOver));
}

Не имеет отношения, но я бы также посоветовал вам использовать flutter format в вашем проекте, чтобы помочь вам со стилем кода.

Другие вопросы по тегам