Load SnackBar before navigating to login page

Issue

I have a signup form and I am trying to make it show the successful SnackBar message first before it navigates to the login page. when I add it to my if statement, it just pushes the login screen without showing any of the Snackbar messages

                   onPressed: ()  {
    
                        if (_formKey.currentState.validate()) {
                          Scaffold.of(context).showSnackBar(SnackBar(
                            content: Text('processing data'),
                          ));

                          User user = User(
                            fullName: fullName.text,
                            phoneNumber: phoneNumber.text,
                            password: password.text,
                          );

                          final database = DatabaseProvider.db;
                          database.insert(user);
                         
                          Scaffold.of(context).showSnackBar(SnackBar(
                            content: Text('registration successful'),
                          ));

                       Navigator.popAndPushNamed(context, Routes.home);
                        }

                        _formKey.currentState.save();
                      },

Solution

This solution worked for me. Chained .closed and .then to the snackbar

onPressed: () {
                        if (_formKey.currentState.validate()) {
                          Scaffold.of(context).showSnackBar(SnackBar(
                            content: Text('processing data'),
                          ));

                          User user = User(
                            fullName: fullName.text,
                            phoneNumber: phoneNumber.text,
                            password: password.text,
                          );

                          final database = DatabaseProvider.db;
                          database.insert(user);

                          Scaffold.of(context)
                              .showSnackBar(SnackBar(
                                content: Text('registration successful'),
                              ))
                              .closed
                              .then((value) => Navigator.popAndPushNamed(
                                  context, Routes.home));
                        }

                        _formKey.currentState.save();
                      },

Answered By – Kellswork

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.