Flutter auto_route | How do I wrap a route with BlocProvider?

3,338

Solution 1

The answer depends on how your routes are constructed, I'll show you how I achieve this.

For nested routes (When you provide children to your routes), you can use a wrapper. You can wrap your bloc provider(s) around the child and it will provide the bloc to all children screens.

/// routes

AutoRoute(
    page: SupportWrapper,
    name: 'SupportRouter',
    path: 'support',
    children: [
        AutoRoute(
            page: HelpSupportScreen,
            path: '',
        ),
        AutoRoute(
            page: MessageUsScreen,
            path: 'issue',
        ),
    ],
),
/// build method of [support_wrapper.dart]

@override
Widget build(BuildContext context) {
  return MultiBlocProvider(
    providers: [
      BlocProvider(
        create: (context) => _supportCubit,
      ),
    ],
    child: const AutoRouter(),
  );
}

If you are not using a wrapper widget, e.g. its a single screen with no children routes, I would create a separate widget to wrap the BlocProvider around the screen.

class SupportScreen extends StatelessWidget {
  const SupportScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => _supportCubit
      child: const _SupportScreen(),
    );
  }
}

class _SupportScreen extends StatelessWidget {
  const _SupportScreen({Key? key}) : super(key: key);

// rest of your screens code...

Solution 2

We have our page widget (state/less/ful) implement AutoRouteWrapper

class HomePage extends StatelessWidget implements AutoRouteWrapper{
  .....
 @override
 Widget wrappedRoute(context){
   return BlocProvider(
            create: (context) => HomeBloc(),
            child:  this, // this as the child Important!
          );
   }

}
Share:
3,338
IncorrectMouse
Author by

IncorrectMouse

Updated on December 30, 2022

Comments

  • IncorrectMouse
    IncorrectMouse over 1 year

    So, I'm using the auto_route package for navigation in my app and flutter_bloc for state management. When I was using the old Navigator, I could just wrap a route with a BlocProvider. For example:

    class Router {
      static Route<dynamic> generateRoute(RouteSettings settings) {
        switch (settings.name) {
          case '/':
            return MaterialPageRoute(
              builder: (_) => BlocProvider( // wrapped Home with BlocProvider
                create: (context) => SubjectBloc(),
                child: Home(),
              ),
            );
          case '/feed':
            return MaterialPageRoute(builder: (_) => Feed());
        }
      }
    }
    

    Now, auto_route uses annotations to generate a routing file. How would I go around providing provider context to the route?

  • IncorrectMouse
    IncorrectMouse almost 3 years
    That something I tried and it works! The developer of the library suggested that I should implement AutoRouteWrapper.
  • mrgnhnt96
    mrgnhnt96 almost 3 years
    Great! if you found my answer helpful, don't forget to upvote it 😉 or mark it as answered if it answered your question
  • Agyakwalf
    Agyakwalf almost 2 years
    this is scoped I realised