【问题标题】:Return data after a pop after a pushReplacementpushReplacement 后弹出后返回数据
【发布时间】:2020-11-19 14:44:35
【问题描述】:

我试图在用户注销时显示一个小吃店(我试图不避免使用 fluttertoast 包)。

在我的欢迎屏幕中,我有一个转到登录页面的按钮:

        child: MaterialButton(
          child: Text('Login', style: TextStyle(color: Colors.white)),
          onPressed: () async {
              final logOutMessage = await Navigator.of(context).pushNamed(LoginScreen.routeName);
              Scaffold.of(context).showSnackBar(SnackBar(
                content: Text(logOutMessage),
                duration: Duration(seconds: 1),
              ));
          }

在我的登录屏幕中,我将路由堆栈推送并替换到另一个页面:

  final authResult = await FirebaseAuth.instance
      .createUserWithEmailAndPassword(email: email, password: password);
  if (authResult.user != null) {
    FirestoreInit.initDocs(authResult.user);
    Navigator.of(context).pushReplacementNamed(Routing.routeName);  // Routing is just another file name where I set up my PageView and Bottom Navigation Bar
  }

在这个路由文件中,一个子小部件是我的主屏幕。我的主屏幕有另一个小部件,它是一个自定义抽屉。在这个客户抽屉中,我有注销按钮:

  ListTile(
    onTap: () {
      FirebaseAuth.instance.signOut();
      // Fluttertoast.showToast(msg: 'You have been signed out');
      Navigator.of(context).pop(ModalRoute.withName(Navigator.defaultRouteName));
    },
    leading: Icon(Icons.exit_to_app),
    title: Text("Log Out"),
  ),

起初,我遇到了著名的脚手架上下文错误。 我去了错误消息中提供的link,并使用了构建器。 快餐栏现在出现,但带有“空”消息。

我假设我在 pop 方法中的消息丢失到另一个页面。 我应该继续使用颤振吐司来做这个吗?

旁注:我不记得我为什么这样写 pop 方法。 我把它改回来了:

  Navigator.of(context).pop(ModalRoute.withName(Navigator.defaultRouteName));

到:

Navigator.of(context).pop('You have logged out!');

我认为路由堆栈应该与小部件树不同,但我觉得它可能与我的小部件树有关,所以我将其包含在内。

在登录屏幕上:

在登录屏幕的 pushReplacement 之后(pop 方法在一个 listtile 中,它是图像底部 CustomerDrawer 下第一个 Column 子项的兄弟):

【问题讨论】:

  • 所以简而言之,您想要的是当用户注销时,他们将返回到默认页面(在您的情况下:登录页面)。在登录页面中,会显示一个小吃栏并写着“您已注销!”?
  • 正确。我已经停止使用snackbar 并开始使用Flushbar 包,因为它更方便且更易于使用。但我也无法使用 Flushbar 包完成它。我只让它与 fluttertoast 一起使用,但我也不再使用它。

标签: flutter


【解决方案1】:

为了将来使用,你可以试试这个:

loginPage.dart

loginAction(BuildContext context) async {
  final result = await Navigator.push(context, MterialPageRoute(
    builder: (context) => secondPage()
  ));

  Scaffold.of(context)..removeCurrentSnackBar();
  if(result != null || result != "") {
    loginScaffoldKey.currentState.showSnackBar(
      SnackBar(
        content: Text(
          result["msg"],
          style: TextStyle(
            color: Colors.white
          ),
        ),
        duration: Duration(
          seconds: result["duration"]
        ),
        backgroundColor: result["color"],
      )
    );
  }
}

secondPage.dart

logoutAction() {
  String msg = "You have logged out!"
  Navigator.pop(
    context,
    {
      "color": Colors.green,
      "msg": msg,
      "duration": 2
    }
  );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多