عمر محمود
عمر محمود

Reputation: 5

Exception has occurred. FlutterError (setState() or markNeedsBuild() called during build

enter image description here

enter image description here

FlutterError (setState() or markNeedsBuild() called during build. 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 on which setState() or markNeedsBuild() was called was: InputPage The widget which was currently being built when the offending call was made was: ReusableCard)

what is the wrong?

Upvotes: 0

Views: 31

Answers (2)

albert
albert

Reputation: 212

Remove the method invocation in the GestureDetector onTap

GestureDetector(
  onTap: (){
   if(onPress != null){
     onPress!();
   }
  },
  child: Container (
    margin: const EdgeInsets.all (15),
    decoration: BoxDecoration(
      color: colour,
      borderRadius: BorderRadius. circular (10.0),
    ), // BoxDecoration
    child: cardchild,
  ), // Container
), //GestureDetector

Upvotes: 0

Slashbin
Slashbin

Reputation: 828

The issue is happening on ReusableCardWidget line 14.

return GestureDetector(
  onTap: onPress!(),

this indicates the onPress function will be triggered while the build is happening. So if you want to trigger it on onTap of GestureDetector use as below

return GestureDetector(
  onTap: onPress,

Or

return GestureDetector(
  onTap: ()=>onPress?.call(),

Since you are assuming onPress function is going to be non null always, make it as VoidCallback onPress and make it required parameter to avoid errors in future.

Upvotes: 0

Related Questions