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/