Scaffold. of() called with a context that does not contain a Scaffold.
This error happens when you use the context of the instantiated scaffold widget. not the context of a child on the scaffold.
Scaffold(
appBar: AppBar(
title:const Text('Example'),
),
body: Center(
child: ElevatedButton(
onPressed: (){
const snackBar = SnackBar(content: Text('Are you talkin\' to me?'));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
child:const Text('SnackBar'),
),
),
);
Let's get Start
You can resolve this error with the use of a different context.
Scaffold(
appBar: AppBar(
title:const Text('Example'),
),
body: Center(
child: Builder(
builder: (context) {
return ElevatedButton(
onPressed: (){
const snackBar = SnackBar(content: Text('Are you talkin\' to me?'));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
child:const Text('SnackBar'),
);
}
),
),
);
After following this step your error is solved.