Currently we're building an app to learn Flutter and Bloc pattern at my company. We use a MultiRepositoryProvider
as the main widget and GoRouter for routing. My route looks like this:
GoRoute(
path: '/game/:id',
builder: (context, state) => GameDetailScreen(),
),
In the MultiRepositoryProvider
the child is a MultiBlocProvider
and the provider for this screen is:
BlocProvider(
create: (BuildContext context) {
return GameDetailBloc(context.read<FirestoreRepo>());
},
),
The BlocProvider's create
function returns the BuildContext
but it's not clear to me how I get the GoRoute state to pass the url param id
to the GameDetailBloc
.
We managed to get this to work by setting the game's id
in GoRoute's build function when creating the GameDetailScreen
. Then we removed that BlocProvider
in the MultiBlocProvider
and then accessed the bloc from the BuildContext
when building the widget but it doesn't seem correct and we're trying to find the "correct solution" to this problem. Any help is greatly appreciated. Thanks!
go_router
has it's params
in its state.Hence pass the state
to the page
GoRoute(
name: "test",
path: "/test/:id",
builder: (context, state) {
return SampleWidget(
goRouterState: state, 👈 Pass state here
);
},
),
context.goNamed("test", params: {"id": "123"}),
class SampleWidget extends StatelessWidget {
GoRouterState? goRouterState;
SampleWidget({super.key, this.goRouterState});
@override
Widget build(BuildContext context) {
print(goRouterState?.params.toString()); 👈 access anywhere like so
return const Scaffold(
body: ...
);
}
}
I couln't completely understand the question. I have attempted to answer based on my interpretation. Let me know the specifics if you have any other requrirements.