I get this error while using bloc not provider.

while trying to add event to SomeBloc that is created before It gives this error :

======== Exception caught by gesture ===============================================================
The following StateError was thrown while handling a gesture:
Bad state: Tried to read a provider that threw during the creation of its value.
The exception occurred during the creation of type EditFooterCubit.

When the exception was thrown, this was the stack: 
#0      _CreateInheritedProviderState.value (package:provider/src/inherited_provider.dart:661:7)
#1      _CreateInheritedProviderState.debugFillProperties (package:provider/src/inherited_provider.dart:750:44)
#2      _InheritedProviderScopeElement.debugFillProperties (package:provider/src/inherited_provider.dart:585:20)
#3      DiagnosticableNode.builder.<anonymous closure> (package:flutter/src/foundation/diagnostics.dart:2945:17)
#4      DiagnosticableNode.builder (package:flutter/src/foundation/diagnostics.dart:2948:8)
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#445b5
  debugOwner: GestureDetector
  state: ready
  won arena
  finalPosition: Offset(188.8, 422.3)
  finalLocalPosition: Offset(36.1, 20.3)
  button: 1
  sent tap down
====================================================================================================

This is where I create the bloc :

class EditFooterPage extends StatelessWidget {
  static int pageNumber() => 5;

  @override
  Widget build(BuildContext context) {

    return BlocProvider<EditFooterCubit>(
      create: (context) => EditFooterCubit(
        footerRepo: RepositoryProvider.of<FooterRepository>(context),
        footerBloc: BlocProvider.of<FooterBloc>(context),
      ),
      child: EditFooterForm(),
    );
  }
}

And here bloc is used to add event :

class EditFooterForm extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: [
            ElevatedButton(
                onPressed: () {
                   //here on button press error happens.
                   context.read<EditFooterCubit>().footerPreviewRequestedToState();
                },
                child: Text('UpdateFooterPart')),
            SizedBox(height: 10),
            FooterPart(),
          ],
        ),
      ),
    );
  }
}

I can't find why It gives error since I created this page like LoginPage and Login Form Example in https://bloclibrary.dev/


Solution 1: Zahra

As the error indicates, error occurred during creation of SomeBloc . So input parameters of SomeBloc are not passed correctly.

For example

SomeBloc({
    @required this.blocA,  
  }) 

SomeBloc needs BLocA and when you want to create SomeBloc you have passed BlocA like this :

BlocProvider<SomeBloc >(
      create: (context) => SomeBloc (//here SomeBloc is the error causing bloc because
          blocA: BlocProvider.of<BlocA>(context),//here BlocA is not accessible in this page or subtree so will give error 
          ),
      child: _View(),
    );

But BLocA is not accessible in this subTree.

So you should Check the input parameters of the bloc that causes error and see whether you have passed the parameters correctly or not because the reason of error is that the passed parameters are not accessible in this subtree.