简体   繁体   English

Go_router 将对象/块传递给新路由

[英]Go_router pass an object/bloc to new route

I use to pass a BLoC instance to a new route like so:我曾经像这样将 BLoC 实例传递给新路由:

Navigator.of(context).push<void(FavoriteDetailPage.route(_favoriteBloc));
class FavoriteDetailPage extends StatelessWidget {
  const FavoriteDetailPage({super.key});
  

  static Route route(FavoriteBloc favoriteBloc) {

    return MaterialPageRoute<void>(
      settings: const RouteSettings(name: 'favorite_detail'),
      builder: (_) => BlocProvider.value(
        value: favoriteBloc,
        child: FavoriteDetailPage(),
      ),
    );
  }

 ...

}

I'm in the process of migrating my app routing to go_router & can't find how to the same.我正在将我的应用程序路由迁移到go_router并且无法找到相同的方法。 -> Provide the same bloc instance to a new route, as go_router parameters can only be String -> 为新路由提供相同的 bloc 实例,因为 go_router parameters只能是String

I could provide the BLoC above my MaterialApp to make it available to my all app but I don't want to provide it to my all app (just to those two sub routes)我可以在我的MaterialApp上方提供 BLoC 以使其可供我的所有应用程序使用,但我不想将它提供给我的所有应用程序(仅提供给这两个子路由)

Use extra parameter in context.goNamed()context.goNamed()中使用extra参数

 GoRoute(
        path: '/sample',
        name: 'sample',
        builder: (context, state) {
         FavoriteBloc favoriteBloc = state.extra as FavoriteBloc ; // -> casting is important
          return FavoriteDetailPage(favoriteBloc : favoriteBloc );
        },
      ),

Receive it as:接收为:

class FavoriteDetailPage extends StatelessWidget {
  FavoriteBloc favoriteBloc;
  const FavoriteDetailPage({super.key,required this.favoriteBloc});
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM