Hi stevediaz933,
This error happens when you try to show a snack bar or alert dialog before the completion of the widget build.
The showDialog method is called inside widget build method, which causes the run time error.
Widget build(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("How are you?"),
);
}); //Error: setState() or markNeedsBuild() called during build.
return Scaffold(
body: Center(
child: Text("Hello World")
)
);
}
You must wrap your code using Future.delayed.
Widget build(BuildContext context) {
Future.delayed(Duration.zero, () {
//your code goes here.
});
return Scaffold(
);
}
Reference:
https://www.fluttercampus.com/guide/230/setstate-or-markneedsbuild-called-during-build/