Flutter 从Flatter中的Navigator.pushNamed访问bloc

Flutter 从Flatter中的Navigator.pushNamed访问bloc,flutter,flutter-bloc,Flutter,Flutter Bloc,我有一个路由如下: routes: { '/': (context) => MainPage(), '/othe_page': (context) => OthePage(), }, Navigator.pushNamed(context, '/othe_page'); 如何将bloc传递给OthePagewidget?1)将bloc作为参数传递 您必须在navigator中将bloc作为参数传递,如下所示 Navigator.pushNamed(context,

我有一个
路由
如下:

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

Navigator.pushNamed(context, '/othe_page');
如何将bloc传递给
OthePage
widget?

1)将bloc作为参数传递

您必须在navigator中将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;
现在,您必须在新页面中接受该集团,如下所示

Navigator.pushNamed(context, '/deletewidget',
                    arguments: bloc);
class _DeleteWidgetState extends State<DeleteWidget> {
var bloc;
@override
Widget build(BuildContext context) {
bloc = ModalRoute.of(context).settings.arguments;
class\u DeleteWidgetState扩展状态{
var集团;
@凌驾
小部件构建(构建上下文){
bloc=ModalRoute.of(context.settings.arguments);
注意:您必须在生成方法外创建bloc变量,并在生成方法中分配bloc变量

-当您返回到最后一个屏幕时,您将无法再访问bloc

2)您可以在父窗口小部件中使用bloc provider,这样您也可以在新屏幕中访问您的bloc。


有关更多详细信息,请查看程序包。

如果要将参数传递给OtherPage(),可以在pushNamed方法.Navigator.pushNamed(上下文,routeName,arguments:)中添加一个命名参数;将bloc作为参数传递可以吗?@FederickJonathanIt最好使用BlocProvider.of(上下文)而不是通过集团本身假设您使用的是颤振集团包