flutterdart

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?


Solution

  • 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.