go_provider 1.1.1
go_provider: ^1.1.1 copied to clipboard
Easily scope providers to routes with GoProviderRoute and ShellProviderRoute.
GoProvider #
GoProvider aims to simplify provider scoping within go_router routes, offering:
- π― Scoped Simplicity: Route-specific state access for cleaner code.
- ποΈ Modular Design: No more 1.000 providers on the top of your app.
- β¨ Auto-Lifecycle: Providers inits on go and auto-disposes on route pop.
- π Streamlined State Management: Effortless integration with
provider
andflutter_bloc
. - π Bus-proof: An easy-to-maintain, single-file library you can copy-paste, just in case.
The Problem #
routes: [
GoRoute(
path: '/',
builder: (context, state) => LoginPage(), // β can't access UserState
),
GoRoute(
path: '/home',
builder: (context, state) => ChangeNotifierProvider( // or BlocProvider
create: (_) => UserState(),
child: const HomePage(), // β
can access UserState
),
routes: [
GoRoute(
path: 'details',
builder: (context, state) => const DetailsPage(), // β throws ProviderNotFoundException
),
],
),
]
GoProvider Solution #
routes: [
GoRoute(
path: '/',
builder: (context, state) => LoginPage(), // β can't access UserState
),
GoProviderRoute(
path: '/home',
providers: [
ChangeNotifierProvider(create: (_) => UserState()), // or BlocProvider
],
builder: (context, state) => const HomePage(), // β
can access UserState
routes: [
GoRoute(
path: 'details',
builder: (context, state) => const DetailsPage(), // β
can access UserState too!
),
],
),
]
You can also use ShellProviderRoute
#
routes: [
ShellProviderRoute(
providers: [
ChangeNotifierProvider(create: (_) => FooState()), // or BlocProvider
],
builder: (context, state, child) => ShellPage(child: child), // β
can access FooState
routes: [
GoRoute(
path: '/a',
builder: (context, state) => const PageA(), // β
can access FooState
),
GoRoute(
path: '/b',
builder: (context, state) => const PageB(), // β
can access FooState
),
],
),
]
Nested #
- You can utilize any widget that extends
SingleChildWidget
, ensuring out-of-the-box compatibility with bothprovider
andflutter_bloc
packages.