setState() or markNeedsBuild() called during build. The relevant error causing widget was RoundIconButton. A RenderFlex overflowed

Viewed 146
int weight = 60;

Widgets under Scaffold:-


Expanded(
              child: Row(
            children: [
              Expanded(
                child: ReusableCard(
                  cardChild: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        'WEIGHT',
                        style: kLabelTextStyle,
                      ),
                      Text(
                        weight.toString(),
                        style: kNumberTextStyle,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          RoundIconButton(
                            onPressed: add,
                            icon: FontAwesomeIcons.minus,
                          ),
                          SizedBox(
                            width: 10.0,
                          ),
                          RoundIconButton(
                            onPressed: add,
                            icon: FontAwesomeIcons.plus,
                          ),
                        ],
                      )
                    ],
                  ),
                  colour: kCardColor,
                ),
              ),
          

class RoundIconButton extends StatelessWidget {
  RoundIconButton({required this.icon, required this.onPressed});

  final IconData icon;
  final Function onPressed;
  @override
  Widget build(BuildContext context) {
    return RawMaterialButton(
      onPressed: onPressed(),
      child: Icon(icon),
      shape: CircleBorder(),
      fillColor: Color(0xff4c4f5e),
      constraints: BoxConstraints.tightFor(
        width: 56.0,
        height: 56.0,
      ),
    );
  }
}

Debug Console:- This InputPage widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.

The widget which was currently being built when the offending call was made was: RoundIconButton dirty

I am trying to add floating buttons, which when pressed increments or decrements the weight displayed. Can anyone help me out as I am new to flutter?

1 Answers

I can only point you into the right direction. This error message means you're triggering a rebuild while another widget is still in the process of being rebuilt. This often happens when you unknowingly trigger a function that sets the state multiple times in a row.

Maybe, just maybe, the problem is with your onPressed parameter. Can you change the variable to be like this:

final VoidCallback onPressed;

And changing the following lines like so:

return RawMaterialButton(
  onPressed: onPressed,
Related