错误状态:试图读取在创建其值期间抛出的提供程序。创建类型 SomeBloc

时间:2021-07-10 10:56:17

标签: flutter

我在使用 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(),
          ],
        ),
      ),
    );
  }
}

我不知道为什么会报错,因为我创建了这个页面,比如 LoginPagehttps://bloclibrary.dev/ 中的登录表单示例

1 个答案:

答案 0 :(得分:0)

如错误所示,在创建 SomeBloc 期间发生错误。 所以SomeBloc的输入参数没有正确传递

例如

SomeBloc({
    @required this.blocA,  
  }) 

SomeBloc 需要 BlocA,当你想创建 SomeBloc 时,你已经像这样传递了 BlocA :

BlocProvider<SomeBloc >(
      create: (context) => SomeBloc (
          blocA: BlocProvider.of<BlocA>(context),//here BlocA is not accessible so will give error 
          ),
      child: _View(),
    );

但是在这个子树中无法访问 BLocA。

所以你应该检查导致错误的块的输入参数。