gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:43:39 25 4
gpt4 key购买 nike

我的类提供程序类中有一个类似这样的 showSnackbar 方法:

  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 上,我给脚手架提供了我从提供者那里使用的相同 key
scaffold(
key: mainProvider.scaffoldkey,
..
...
我拥有的每个选项卡都使用相同的小部件,当按下小部件时,将调用此 snackbar 。
如果我尝试像这样导航回标签屏幕:
                    Navigator.of(context)
.pushReplacementNamed(TabsScreen.id);
从标签栏应用程序栏的屏幕内的屏幕它给了我这个错误..我该怎么办?

最佳答案

正如文档所说,要从内部屏幕返回,您应该使用Navigator.pop(context);编辑:
好的,在这种情况下,最好的解决方案似乎是使用 Navigator.popUntil(context, ModalRoute.withName('screen_route'));功能。

关于 flutter :多个小部件使用相同的 GlobalKey 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62676117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com