Skip to content Skip to sidebar Skip to footer

Flutter: Blocprovider.of() Called With A Context That Does Not Contain A Bloc Of Type

I am new to flutter and i wanted to implement a simple Login screen using BLoc. There is no build error but in runtime the following error is received 'blocprovider.of() called wit

Solution 1:

Did you "provide" the LoginBloc in a widget above LoginForm?

This error means that there is no parent widget referencing a created LoginBloc.

If you don't you'll need to have:

BlocProvider<LoginBloc>(
  create: (context) =>LoginBloc(),
  builder: (context, state) {
    // LoginForm can now use `BlocProvider.of<LoginBloc>(context)`
  }
)

Solution 2:

Make sure that LoginForm is under LoginBloc widget tree from dev tools. May be you are using Navigator.push() from the AuthPage to open new login form. If you are doing like this, then loginForm will be rendered as a new page again without LoginPage as a parent. So try to keep LoginForm under LoginScreen.

Your Widget tree should look like below:

--LoginScreen--LoginBloc--LoginForm

And also make sure that you created LogicBloc as @jack said in first comment:

BlocProvider<LoginBloc>(
  create: (context) =>LoginBloc(),
  builder: (context, state) {
    // LoginForm can now use `BlocProvider.of<LoginBloc>(context)`
  }
)

Post a Comment for "Flutter: Blocprovider.of() Called With A Context That Does Not Contain A Bloc Of Type"