Flutter Bloc:从上下文开始找不到祖先吗?

文森特·吉约(Vincent Guillois)

我是Flutter的新手,也是Bloc使用的新手。

编译代码时出现错误:

    The following assertion was thrown building Login(dirty, state: _LoginFormState#44e7f):
         BlocProvider.of() called with a context that does not contain a Bloc of type BtnBloc.
         No ancestor could be found starting from the context that was passed to
         BlocProvider.of<BtnBloc>().
       This can happen if the context you use comes from a widget above the BlocProvider.
         This can also happen if you used BlocProviderTree and didn't explicity provide 
         the BlocProvider types: BlocProvider(bloc: BtnBloc()) instead of BlocProvider<BtnBloc>(bloc:
         BtnBloc()).
         The context used was: Login(dirty, state: _LoginFormState#44e7f)    

我试图在我的bloc类中更改一些内容,但是对此没有任何作用。

这是我的博客课程:

class BtnBloc extends Bloc<BtnEvent, BtnState> {
  @override
  BtnState get initialState => BtnState.initial();

  @override
  Stream<BtnState> mapEventToState(
      BtnState currentState, BtnEvent event) async* {
    if (event is IdleEvent) {
      yield currentState..state = 1;
    } else if (event is LoadingEvent) {
      yield currentState..state = 1;
    } else if (event is RevealEvent) {
      yield currentState..state = 2;
    }
  }
}

这是我的构建方法:

final _btnBloc = new BtnBloc();

  _LoginFormState(
      {Key key,
      this.primaryColor,
      this.backgroundColor,
      this.backgroundImage,
      this.logo});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        bloc: _btnBloc,
        child: BlocBuilder<BtnEvent, BtnState>(
            bloc: BlocProvider.of<BtnBloc>(context),
            builder: (context, BtnState state) {
return myWidget();

请帮我 :'(

填充堆栈

下面的行是您的问题。该错误正是消息所显示的内容。

...
child: BlocBuilder<BtnEvent, BtnState>(
     bloc: BlocProvider.of<BtnBloc>(context), <------- Problem line
     builder: (context, BtnState state) {
...

您在此处使用的上下文没有附加BlocBuilder,因此没有什么东西将BloC从顶部向下传递到您的LoginState。你可以

  1. 使用现有的块(_btnBloc)并将其传递(推荐
  2. 将窗口小部件包裹起来,并使用BlocProvider在屏幕上显示LoginState,以便您可以在窗口小部件状态下访问它。

如果这是您的第一个视图,请使用1。

更改

 bloc: BlocProvider.of<BtnBloc>(context), <------- Problem line

bloc: _btnBloc

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Flutter 问题:从上下文开始找不到 MediaQuery 祖先

Flutter HookWidget 从上下文中读取未知

Flutter Dart Bottomsheet错误上下文=上下文

Flutter:找不到MediaQuery小部件祖先

Flutter:使用不包含Bloc类型的上下文调用blocprovider.of()

Flutter - 如何获取当前上下文?

Flutter Navigator.pushNamed()上下文

在Flutter中获取全局上下文

当使用flutter_bloc库在Flutter中推送新路线时,上下文无法正常工作

Flutter错误-找不到材料祖先的特定小部件为:

在父级或祖先上下文中找不到方法

如何调试“在Android的父级或祖先上下文中找不到方法showTimePickerDialog(View)”?

Flutter:BottomNavigationBar文件为上下文提供了错误

Flutter Navigation未定义的“上下文”

Flutter计算功能的上下文/范围是什么

Flutter在initState方法中获取上下文

创建StatefulWidget时访问Flutter上下文

无法在Flutter中实现上下文感知图标按钮

在Dart / Flutter中定义变量并引用父上下文

Flutter:如何从 Dismissible onDismissed 访问上下文

Flutter-如何从MockRepository获取上下文?

Flutter中没有上下文的AlertDialog

如何传递上下文以在Flutter中显示AlertDialog

Flutter:我的动态菜单有上下文问题

Flutter 错误:未定义的名称“上下文”

Flutter 錯誤:68:14:錯誤:找不到 Getter:“上下文”。和未處理的異常:“空”類型不是“BuildContext”類型的子類型

找不到Flutter命令

找不到上下文的DownstreamContext

毕加索 - 找不到“上下文”