go_provider 1.0.0
go_provider: ^1.0.0 copied to clipboard
Easily scope providers to routes with GoProviderRoute and ShellProviderRoute.
GoProvider #
This package aim to provide a simple way to scope providers to a route in go_router. Making it possible to:
- Scope routes: access a provider in a route and it's sub-routes.
- Auto dispose: the provider is disposed when the route is popped.
- Guarded shell routes: the
redirect
property was added to guard shell routes.
This package is compatible with go_router
v10.0.0 and above. This package has 100 lines of code, you can copy and paste it in your project. Modify it to your needs.
Usage #
routes: [
GoRoute(
path: '/login',
builder: (context, state) => MyLoginPage(), // can't access UserNotifier
),
GoProviderRoute(
path: '/home',
builder: (context, state) => MyPage(), // can access UserNotifier
providers: [
ChangeNotifierProvider(create: (context) => UserNotifier()),
],
routes: [
GoRoute(
path: '/settings',
builder: (context, state) => MySettingsPage(), // can access UserNotifier
),
],
),
]
You can also use ShellProviderRoute
:
routes: [
ShellProviderRoute(
builder: (context, state, child) => MyPage(child: child), // can access OtherNotifier
providers: [
ChangeNotifierProvider(create: (context) => OtherNotifier()),
],
routes: [
GoRoute(
path: '/page1',
builder: (context, state) => MyPage1(), // can access OtherNotifier
),
GoRoute(
path: '/page2',
builder: (context, state) => MyPage2(), // can access OtherNotifier
),
],
),
]
Nested #
- You can actually use any Widget that extends
SingleChildWidget
. Which makes it compatible withprovider
andflutter_bloc
packages right out of the box.