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
345 views
in Technique[技术] by (71.8m points)

flutter - snackbar message does not pop up

I have following code that updates the data on firebase but due to some reasons, it does not show me message which was triggered by snackbar.

var user = FirebaseFirestore.instance
      .collection("tblusers")
      .doc(currentUser.uid)
      .update({
    "first_name": "Test"
  }).then((value) => () {
    final snackBar = SnackBar(
        content: Text("Saved successfully"));
    Scaffold.of(context).showSnackBar(snackBar);
  });

Below is the full source code. First name is being updated successfully. But, the promise part is not executing due to some reasons. Am I missing anything?

Class 1st in separate file

class AccountSettingsScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Body(),
    );
  }
}

Class 2nd in separate file

class Body extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Center(
            child: SingleChildScrollView(
                child: Column(children: <Widget>[
      AccountForm()
    ]))));
  }
}

Class 3rd in separate file

class AccountForm extends StatelessWidget {
      @override
      Widget build(BuildContext context) {    
        return Form(
            autovalidateMode: AutovalidateMode.onUserInteraction,
            child: Column(children: <Widget>[
              Container(
                  child: Column(children: [
                    TextFormField(),
                    ElevatedButton(
                        onPressed: () {
                            var currentUser = FirebaseAuth.instance.currentUser;
                            if (currentUser != null) {
                              var user = FirebaseFirestore.instance
                                  .collection("tblusers")
                                  .doc(currentUser.uid)
                                  .update({
                                "first_name": "Test A"
                              }).then((value) => () {
                                    final snackBar = SnackBar(
                                        content: Text("Saved successfully"));
                                    Scaffold.of(context).showSnackBar(snackBar);
                                  });
                            }
                        },
                        child: Text("Submit"))
                  ]))
            ]));
    }
}
question from:https://stackoverflow.com/questions/65844166/snackbar-message-does-not-pop-up

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

1 Answer

0 votes
by (71.8m points)

I think the main issue is that you are calling Scaffold.of(context) but there really is no scaffold in your widget tree. Wrap the Form widget in Scaffold like this:

  Widget build(BuildContext context) {    
    return Scaffold(
            body: Form( ...

If this doesn't help i suggest you wrapping ElevatedButton in Builder, like this:

Builder(
 builder: (context) => ElevatedButton(...

Note that Scaffold.of(context).showSnackbar was deprecated after v1.23.0-14.0.pre and thus you should use ScaffoldMessenger.of(context).showSnackBar.

Edit:

class AccountForm extends StatelessWidget {
  void onSubmit(BuildContext context) async {

    var currentUser = FirebaseAuth.instance.currentUser;
    if (currentUser != null) {
      var user = FirebaseFirestore.instance
        .collection("tblusers")
        .doc(currentUser.uid)
        .update({
          "first_name": "Test A"
        }).then((value) {
          ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Saved successfully")));
        });
     } 
  }

  @override
  Widget build(BuildContext context) {
    return Form(
      autovalidateMode: AutovalidateMode.onUserInteraction,
      child: Column(
        children: <Widget>[
          Container(
            child: Column(
              children: [
                TextFormField(),
                Builder(
                  builder: (context) => ElevatedButton(
                    onPressed: () => onSubmit(context),
                    child: Text("Submit"),
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}

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

...