0

I recently started with AutoRoute and trying to make it work along with flutter_bloc.

I have followed the instructions from https://github.com/Milad-Akarie/auto_route_library/issues/1590 and able to make it work! However I am facing an issue if I refactor and introduce a child widget. Please help

My set up


@AutoRouterConfig(replaceInRouteName: 'Page,Route')                    
class AppRouter extends $AppRouter{    
    
  @override      
  List<AutoRoute> get routes => [                  
    AutoRoute(                    
      path: '/a',                    
      page: ParentRouteWrapper.page,                    
      children: [                    
        AutoRoute(path: 'a', page: ARoute.page),                    
        AutoRoute(path: 'b', page: BRoute.page),                           
      ],                    
    ),                           
  ],       
 }   

and

@RoutePage
class ParentRouteWrapper extends AutoRouter implements AutoRouteWrapper {                
  @override                
  Widget wrappedRoute(BuildContext context) {     
// this gets called once as expected           
    return BlocProvider<YourBloc>(create: (_) => YourBloc(), child: this);                
  }            
}    

@RoutePage
class A extends StatelessWidget {
  Widget build(BuildContext context) {
    // consume the injected instance of YourBloc here
    return BlocConsumer<SubjectBloc, SubjecttState>(
       listener: (context, state) {

   if (state is SubjectReadyState) {
       context.router.push(const BRoute());
   }
......
 },
  }
}

@RoutePage
class B extends StatelessWidget {
  Widget build(BuildContext context) {
    // consume the injected instance of YourBloc here
  }
}

In this set up everything works perfectly and following gets called once as expected


 Widget wrappedRoute(BuildContext context) {     
// this gets called once as expected           
    return BlocProvider<YourBloc>(create: (_) => YourBloc(), child: this);                
  }     

Now if I introduce a child widget and call context.router.push(const BRoute()) from there the wrappedroute gets called again. Is this the expected behavior? I would expect it not to be called again as the router is is already initialized and wrapped

The set up where it does not work is as following: Everything is same as above except class A. class A now have a child widget without RoutePage annotation:


@RoutePage
class A extends StatelessWidget {
  Widget build(BuildContext context) {
return AChild();
}
   
}

//No Route Page annotation here
class AChild extends StatelessWidget {
  Widget build(BuildContext context) {
    // consume the injected instance of YourBloc here
    return BlocConsumer<SubjectBloc, SubjecttState>(
       listener: (context, state) {

   if (state is SubjectReadyState) {
       context.router.push(const BRoute());
   }
......
 },
  }
}

 Widget wrappedRoute(BuildContext context) {     
// this gets called again! not expected           
    return BlocProvider<YourBloc>(create: (_) => YourBloc(), child: this);                
  }     

In this set up the Parent and the wrappedRoute gets called again and a new instance of bloc gets created which is not what I want

Any help would be greatly appreciated

Abhijit Mazumder
  • 8,641
  • 7
  • 36
  • 44

0 Answers0