在导航到登录页面之前加载 SnackBar

Load SnackBar before navigating to login page

我有一个注册表单,我试图让它在导航到登录页面之前先显示成功的 SnackBar 消息。当我将它添加到我的 if 语句时,它只是推送登录屏幕而不显示任何 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'),
                          ));

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

                        _formKey.currentState.save();
                      },

你可以这样做:

onPressed: () async {

                    if (_formKey.currentState.validate()) {
                      await 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;
                      await database.insert(user);
                     
                      await Scaffold.of(context).showSnackBar(SnackBar(
                        content: Text('registration successful'),
                      ));

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

                    _formKey.currentState.save();
                  },

这个解决方案对我有用。链接 .closed.then 到小吃店

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();
                      },