Skip to main content

Errors | Flutter

  • Errors | Flutter

Resolve 'Flutter SetState Called During Build' Issue 💣

Calling the setState method before the build method has completed can cause the "setState() or markNeedsBuild() called during build" error. This happens because the Flutter framework is in the middle of building the widget tree and changing the state during this process can lead to inconsistent or unexpected behaviour.

Fix for this issue:

 Future.delayed(Duration.zero, () async {
myFunction();
});

//
if (!mounted)
//

WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.push(context, MaterialPageRoute(builder: (_) => const Homepage()));
});

alt text