【问题标题】:Bad state: Tried to read a provider that threw during the creation of its value. The exception occurred during the creation of type SomeBloc错误状态:试图读取在创建其值期间抛出的提供程序。在创建类型 SomeBloc 期间发生异常
【发布时间】: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(),
          ],
        ),
      ),
    );
  }
}

我找不到为什么它给出错误,因为我创建了这个页面,如LoginPagehttps://bloclibrary.dev/ 中的登录表单示例

【问题讨论】:

  • 能否提供EditFooterCubit的代码,可能是你的问题

标签: flutter


【解决方案1】:

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

例如

SomeBloc({
    @required this.blocA,  
  }) 

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

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(),
    );

但 BLocA 在此子树中不可访问。

所以你应该检查导致错误的bloc的输入参数 看看你是否正确传递了参数,因为错误的原因是传递的参数在这个子树中是不可访问的。

【讨论】:

    猜你喜欢
    • 2021-06-25
    • 2022-01-23
    • 1970-01-01
    • 2020-05-22
    • 2019-09-23
    • 2018-11-20
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    相关资源
    最近更新 更多