颤振:多个小部件使用相同的 GlobalKey 错误

编程入门 行业动态 更新时间:2024-10-09 22:23:25
本文介绍了颤振:多个小部件使用相同的 GlobalKey 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的类提供程序类中有一个 showSnackbar 方法,如下所示:

i have a showSnackbar method that looks like this in my class provider class:

  GlobalKey<ScaffoldState> scaffoldKey =
      GlobalKey<ScaffoldState>(debugLabel: 'scaffoldKey');

  void showSnackBarE(String label) {
    if (purchasedItems[label] != 0) {
      final snackBar = SnackBar(
        content: Text("$label has already been added to cart!"),
        backgroundColor: Colors.black87,
        behavior: SnackBarBehavior.fixed,
        duration: Duration(seconds: 1),
      );
      scaffoldKey.currentState.removeCurrentSnackBar();
      scaffoldKey.currentState.showSnackBar(snackBar);
    } else {
      final snackBar = SnackBar(
        action: SnackBarAction(
            label: "Undo",
            onPressed: () {
              purchasedItems[label] = 0;
              getTotalSum();
            }),
        content: Text("$label has been added to cart!"),
        backgroundColor: Colors.black87,
        behavior: SnackBarBehavior.fixed,
        duration: Duration(seconds: 1),
      );
      scaffoldKey.currentState.removeCurrentSnackBar();
      scaffoldKey.currentState.showSnackBar(snackBar);
    }

    notifyListeners();
  }

在我的 TabsScreen 中,我为脚手架提供了我从提供者那里使用的相同密钥

at my TabsScreen im giving the scaffold the same key i used from provider

scaffold(
key: mainProvider.scaffoldkey,
..
...

我拥有的每个标签都使用相同的小部件,当按下小部件时,将调用此小吃栏..

every tab i have uses the same widget in which when the widget is pressed this snackbar will be called..

如果我尝试像这样导航回标签屏幕:

if i try to navigate back to the tabs screen like this:

                    Navigator.of(context)
                        .pushReplacementNamed(TabsScreen.id);

从标签栏应用程序栏的屏幕内的屏幕上它给了我这个错误..我该怎么办?

from a screen inside a screen from the tab bar appbar it gives me this error.. what should i do?

推荐答案

正如文档所说从屏幕里面返回你应该使用Navigator.pop(context);

As docs say to go back from the screen inside you should use Navigator.pop(context);

好的,在这种情况下,似乎最好的解决方案是使用 Navigator.popUntil(context, ModalRoute.withName('screen_route')); 函数.

Ok so it seems that in this case the best solution is to use the Navigator.popUntil(context, ModalRoute.withName('screen_route')); function.

这篇关于颤振:多个小部件使用相同的 GlobalKey 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 10:51:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408632.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   部件   错误   颤振   GlobalKey

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!