Could not find the correct Provider<T> above this BlocConsumer<T, S> Widget

Issue

First of all, I read almost all question related to this error and I couldn’t find any answer to question.

I’m trying to use BlocProvider to provide a child widget with BlocProvider in SignInPage:

class SignInPage extends StatelessWidget {
  const SignInPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Sign In'),
      ),
      body: BlocProvider(
        create: (context) => getIt<SignInFormBloc>(),
        child: const SignInForm(),
      ),
    );
  }
}

And the SignInForm is:

class SignInForm extends StatelessWidget {
  const SignInForm({Key? key}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return BlocConsumer<SignInFormBloc, SignInFormState>(
      listener: (context, state) {
        
      },
      builder: (context, state) {
        
      },
    );
  }
}

As I know, the BlocConsumer should search for the SignInFormBloc in parent widget, and the parent widget is BlocProvider which I provide it with SignInFormBloc I don’t know why it couldn’t find it

I try to wrap it in Builder(builder: (context) {} ) in case it searchs in wrong context but doesn’t work too.

Solution

I made a mistake in AppRouter instated of using SignInPage I used SignInForm

@MaterialAutoRouter(

  routes: [
    AutoRoute(page: SplashPage, initial: false),
    // It should be SignInPage -_-
    AutoRoute(page: SignInForm, initial: true),
  ],
)
class $AppRouter {}

Answered By – Mahmood Ali

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.