Source codeClick here to view the full source code of this example
This AutoRoute example shows how to share a provider between multiple pages without scoping it for the entire app.
This can be considered as a more advanced example and can be used when you want to share a provider between multiple pages without scoping it for the entire app.
Sometimes placing a provider above the MaterialApp is needed and perfectly fine, but placing everything above MaterialApp can lead to a messy codebase, because maintainers can access every provider from every place, causing spaghetti code.
Dependency
This is the auto_route version used in this example:
File structure
Let’s look at the file structure:
Directorycontrollers
Directorybooks
controller.dart
model.dart
Directorypages
book.dart
books.dart
home.dart
main.dart
router.dart
router.gr.dart
Models
Let’s start with the model:
BooksController
Then let’s create the controller:
Router
Let’s go to the router.
BooksWrapperPage
The BooksWrapperRoute is where the provider is placed. The provider is shared between BooksRoute and BookRoute because the routes are children of BooksWrapperRoute.