【发布时间】:2021-07-10 10:56:17
【问题描述】:
我在使用 bloc 而不是提供者时收到此错误。
在尝试将事件添加到之前创建的 SomeBloc 时出现此错误:
======== 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
====================================================================================================
这是我创建集团的地方:
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(),
);
}
}
这里使用 bloc 来添加事件:
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(),
],
),
),
);
}
}
我找不到为什么它给出错误,因为我创建了这个页面,如LoginPage 和https://bloclibrary.dev/ 中的登录表单示例
【问题讨论】:
-
能否提供EditFooterCubit的代码,可能是你的问题
标签: flutter