0

I am trying to show snackbar on button click but due to some reasons facing an error message below.

Unhandled Exception: Scaffold.of() called with a context that does not contain a Scaffold.

Am I missing anything?

Code

class SignIn extends StatefulWidget {
  @override
  _SignInState createState() {
    return _SignInState();
  }
}

class _SignInState extends State<SignIn> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: "Hello",
        home: Scaffold(
            body: Center(
                child: ListView(shrinkWrap: true, children: <Widget>[
              Center(
                  child: Form(
                key: _formKey,
                child: Column(children: <Widget>[                 
                  Container(
                    child: Column(
                      children: <Widget>[                        
                        Container(
                          child: Row(
                            children: <Widget>[
                              ElevatedButton(
                                  child: Text("Login"),
                                  onPressed: () {
                                    Scaffold.of(context).showSnackBar(
                                            SnackBar(
                                              content: Text("Hello there!"),
                                            ),
                                          );
                                  })
                            ],
                          ),
                        )
                      ],
                    ),
                  )
                ]),
              ))
            ]))));
  }
}
Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • Does the `onPressed` callback function need to be async? An async call will be called from a different thread adn will it get the proper Scaffold context that time? Could you try making that callback normal synchronous only. – Sisir Jan 10 '21 at 06:50
  • Sorry, that was a type. There should be no async with onPress. – Pankaj Jan 10 '21 at 07:30
  • Does this answer your question? [Scaffold.of() called with a context that does not contain a Scaffold](https://stackoverflow.com/questions/51304568/scaffold-of-called-with-a-context-that-does-not-contain-a-scaffold) – RaSha Jan 10 '21 at 09:43
  • This will not work because `showSnackBar` is depreciated – Pankaj Jan 11 '21 at 03:36

2 Answers2

-1

Use Scaffold key for showing snackbar.

class SignIn extends StatefulWidget {
  @override
  _SignInState createState() {
    return _SignInState();
  }
}

class _SignInState extends State<SignIn> {
  final _formKey = GlobalKey<FormState>();
  final _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Hello",
      home: Scaffold(
        key: _scaffoldKey,
        body: Center(
          child: ListView(
            shrinkWrap: true,
            children: <Widget>[
              Center(
                child: Form(
                  key: _formKey,
                  child: Column(children: <Widget>[
                    Container(
                      child: Column(
                        children: <Widget>[
                          Container(
                            child: Row(
                              children: <Widget>[
                                ElevatedButton(
                                    child: Text("Login"),
                                    onPressed: () async {
                                      _scaffoldKey.currentState.showSnackBar(
                                        SnackBar(
                                          content: Text("Hello there!"),
                                        ),
                                      );
                                    })
                              ],
                            ),
                          )
                        ],
                      ),
                    )
                  ]),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
-1

Try

Material App (home: NewWidget())

In which the

NewWidget();

is a stateless or stateful widget that returns Scaffold

Create a new widget and paste all the code from Scaffold. Then return the widget at home:

Clinton
  • 435
  • 1
  • 4
  • 13