0

I have the follwing code with a _checkCreds function. I want to show an alert when that button is pressed.

When I replace the print() statement with an AlertDialog(), I get a "No MaterialLocalizations found".

void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
 @override
 _MyAppState createState() => _MyAppState(); 
}
class _MyAppState extends State<MyApp> {
 void _checkCreds(bool val) {
 if(!val){
 print("Warning, show alert here instead of print!");
 return;
 }
 ...
 // Continue executing
 }
 Widget build(BuildContext context) {
 return MaterialApp(
 home: RaisedButton(
 child: "Press me to trigger an alert!" ),
 onPressed: () => _checkCreds(false),
 ); 
 } 
}
asked Jul 24, 2020 at 14:41
4

2 Answers 2

2

I've figured it out, coming for a React environment I didn't know where "BuildContext" fits in all of this. After further investigation I passed the current context as an argument to the function that calls the Alert.

Above main():

Future<void> _ackAlert(BuildContext context) {
 return showDialog<void>(
 context: context,
 builder: (BuildContext context) {
 return AlertDialog(
 title: Text('Error'),
 content: const Text('Please enter a valid text'),
 actions: <Widget>[
 FlatButton(
 child: Text('Okay'),
 onPressed: () {
 Navigator.of(context).pop();
 },
 ),
 ],
 );
 }, 
 ); 
}

Inside the main widget:

Widget build(BuildContext context) {
 return MaterialApp(
 home: RaisedButton(
 child: "Press me to trigger an alert!" ),
 // I've added a parameter that takes the current context
 onPressed: () => _checkCreds(false, context),
 ); 
 } 
answered Jul 24, 2020 at 15:26
Sign up to request clarification or add additional context in comments.

2 Comments

Great @Anis Benna, +1 for the efforts and learning!
I'm sure it works but i think is an antipattern
1

I think this is what you want:

void _checkCreds(bool val){//put it inside 'MyAppState' class
 if(!val){
 showDialog(
 barrierDismissible: true,//tapping outside dialog will close the dialog if set 'true'
 context: context, 
 builder: (context){
 return Dialog(
 //Add code here
 );
 }
 );
 }
 ...
 // Continue executing
}

AlertDialog and Dialog has same properties except AlertDialog has content property whereas Dialog has child property. Both does same work.

answered Jul 24, 2020 at 15:50

1 Comment

The problem wasn't the Alert in itself, however the fact that it needs a "context" which should be passed to the function: _checkCreds(bool val, BuildContext context). If I pass the context in the button OnPressed, the issue is solved.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.