【问题标题】:access bloc from Navigator.pushNamed in flutter在颤振中从 Navigator.pushNamed 访问 bloc
【发布时间】:2020-05-13 02:51:28
【问题描述】:

我有一个这样的routing

routes: {
    '/': (context) => MainPage(),
    '/othe_page': (context) => OthePage(),
},

Navigator.pushNamed(context, '/othe_page');

如何将 bloc 传递给 OthePage 小部件?

【问题讨论】:

  • 如果你想将参数传递给 OthePage(),你可以在 pushNamed 方法中添加一个命名参数。 Navigator.pushNamed(context, routeName, arguments: );
  • 将 bloc 作为参数传递好吗? @FederickJonathan
  • 如果你使用 Flutter_Bloc 包,最好使用 BlocProvider.of(context) 而不是传递 bloc 本身

标签: flutter flutter-bloc


【解决方案1】:

1) 将 bloc 作为参数传递

您必须将您的集团作为参数在导航器中传递,如下所示。

Navigator.pushNamed(context, '/deletewidget',
                    arguments: bloc);

现在您必须在新页面中接受该集团,如下所示。

class _DeleteWidgetState extends State<DeleteWidget> {
var bloc;
@override
Widget build(BuildContext context) {
bloc = ModalRoute.of(context).settings.arguments;

注意:你必须在 build 方法中创建 bloc 变量,并在 build 方法中分配 bloc 变量。

-当您返回上一个屏幕时,您将无法再访问 bloc。

2) 您可以在父窗口小部件中使用 bloc 提供程序,以便您也可以在新屏幕中访问您的 bloc。

更多详情请查看Flutter_bloc 包。

【讨论】:

    猜你喜欢
    • 2020-10-14
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 2021-08-06
    • 2022-10-01
    • 1970-01-01
    相关资源
    最近更新 更多