Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

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

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!"),
                                            ),
                                          );
                                  })
                            ],
                          ),
                        )
                      ],
                    ),
                  )
                ]),
              ))
            ]))));
  }
}
question from:https://stackoverflow.com/questions/65650689/unhandled-exception-scaffold-of-called-with-a-context-that-does-not-contain-a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!"),
                                        ),
                                      );
                                    })
                              ],
                            ),
                          )
                        ],
                      ),
                    )
                  ]),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...