Bloc: Unable to retrieve Bloc from context in child widget

Created on 25 Jan 2020  路  2Comments  路  Source: felangel/bloc

Describe the bug
I may be missing something here, but my understanding is that declaring a Bloc in a parent widget's context should make it available to the subt tree. It does not seem to be working for me. I get the error

BlocProvider.of() called with a context that does not contain a Bloc of type CreateFormBloc

Parent Class:
`class ScreenWrapper extends StatelessWidget {
ScreenWrapper({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return BlocBuilder(
builder: (context, state) {
if (state is LoadingState || state is InitialState || state is SavingState) {
return Center(child: CircularProgressIndicator());
}
if (state is LoadedState) {
return AScreen();
}
if (state is CreateRequestedState) {
return BlocProvider(
create:(BuildContext context) =>
CreateFormBloc(repository: RepositoryProvider.of(context)),
child: CreateScreen(),
);
}
if (state is SearchState) {
return SearchScreen();
}
}
}
`
In the CreateScreen, using BlocProvider.of fails with the error. BlocProvider.of() called with a context that does not contain a Bloc of type CreateFormBloc

I'm not sure what I'm missing here.

question

Most helpful comment

Thank you! Was missing the proper declaration of the Repo Provider, also nested the CreateScreen in another widget.

All 2 comments

Hi @franagu 馃憢
Thanks for opening an issue!

It looks like you're not accessing the repository correctly. You need to specify the type of repository when you perform the lookup RepositoryProvider.of<MyRepository>(context),

Also make sure you're providing the bloc type BlocProvider.of<CreateFormBloc>(context).

Let me know if that helps!

Thank you! Was missing the proper declaration of the Repo Provider, also nested the CreateScreen in another widget.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

krusek picture krusek  路  3Comments

nhwilly picture nhwilly  路  3Comments

rsnider19 picture rsnider19  路  3Comments

timtraversy picture timtraversy  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments