Flutter Bloc:在创建的上下文中没有我的 bloc 的祖先,而我很好地为其提供了 BlocProvider.value

Posted

技术标签:

【中文标题】Flutter Bloc:在创建的上下文中没有我的 bloc 的祖先,而我很好地为其提供了 BlocProvider.value【英文标题】:Flutter Bloc : There is no ancestor of my bloc in the context founded while I have it well provided with a BlocProvider.value 【发布时间】:2021-12-21 05:17:22 【问题描述】:

这里是我创建 Bloc(主文件)的地方:

if (state is AuthenticationAuthenticated) 
  return BlocProvider<NavigateHomeScreenBloc>(
      create: (context) => NavigateHomeScreenBloc(state.user!.uid)
        ..add(NavigateToProjects()),
      child: Home());

这里是我显示带有浮动按钮的页面(主页):

if (state is NavigateHomeScreenDemandesScreen)
  return Demandes(state.demandesPageModel);

Demandes 页面的 Scaffold 底页:

bottomSheet: BlocProvider.value(
  value: BlocProvider.of<NavigateHomeScreenBloc>(context),
  child: AddCollaboratorButton(),
),

我的浮动按钮将我的页面推送到发生祖先错误的位置(我的 BlocProvider.value 在这里 -> AddCollaboratorButton 类):

FloatingActionButton(
  onPressed: () 
    Navigator.of(context)
      .push(MaterialPageRoute(
        builder: (context) => BlocProvider.value(
          value:
          BlocProvider.of<NavigateHomeScreenBloc>(context),
          child: DemandeCollaboration())))
      .then((value) =>
            BlocProvider.of<NavigateHomeScreenBloc>(context)
            .add(NavigateToDemandes()));
  ,
  child: Icon(Icons.add, color: Colors.white),
),

我在 _DemandeCollaborationState 上尝试执行的操作会导致我的错误(DemandeCollaboration 页面):

DatabaseService databaseService = DatabaseService(
        uid: BlocProvider.of<NavigateHomeScreenBloc>(context).uid);

这是我点击浮动按钮时的错误:

Screen shot error

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building Builder(dirty):
        BlocProvider.of() called with a context that does not contain a NavigateHomeScreenBloc.

        No ancestor could be found starting from the context that was passed to BlocProvider.of<NavigateHomeScreenBloc>().

        This can happen if the context you used comes from a widget above the BlocProvider.

        The context used was: Builder(dirty)

The relevant error-causing widget was
MaterialApp
lib/main.dart:65
When the exception was thrown, this was the stack
#0      BlocProvider.of
package:flutter_bloc/src/bloc_provider.dart:103
#1      _AddCollaboratorButtonState.build.<anonymous closure>.<anonymous closure>
package:app_apporteur_affaires/…/widgets/addCollaboratorButton.dart:34
#2      MaterialPageRoute.buildContent
package:flutter/…/material/page.dart:54
#3      MaterialRouteTransitionMixin.buildPage
package:flutter/…/material/page.dart:107
#4      _ModalScopeState.build.<anonymous closure>.<anonymous closure>
package:flutter/…/widgets/routes.dart:840
...
════════════════════════════════════════════════════════════════════════════════

【问题讨论】:

你在哪里创建你的集团? 在我的主文件中,对我来说祖先树受到尊重,当我使用浮动按钮在文件中调用它时,我可以访问 BlocProvider.of(context) 【参考方案1】:

好的,我终于找到了解决方案,我已经在 Navigator.of(context).push 之前定义了我的 bloc,如下所示:

var myNagigateBloc = BlocProvider.of<NavigateHomeScreenBloc>(context);

我已经用 myNagigateBloc 替换了 BlocProvider.value 的值,如下所示:

onPressed: () 
  Navigator.of(context)
      .push(MaterialPageRoute(
          builder: (context) => BlocProvider.value(
              value: myNagigateBloc,
              child: DemandeCollaboration())))
      .then((value) =>
          BlocProvider.of<NavigateHomeScreenBloc>(context)
              .add(NavigateToDemandes()));
,

我认为问题在于我的 BlocProvider.value 在一个新的上下文中,来自 MaterialPageRoute 的构建。

【讨论】:

以上是关于Flutter Bloc:在创建的上下文中没有我的 bloc 的祖先,而我很好地为其提供了 BlocProvider.value的主要内容,如果未能解决你的问题,请参考以下文章

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

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

没有 BLoC 的 Flutter 状态管理

使用 BLoC 的 Flutter 导航:用于从 Navigator 推送路由的上下文必须是 Navigator 小部件的后代

flutter - bloc - 我如何在我的 Ui 中使用 FutureBuilder 来正确实现 Bloc 架构

Flutter - Scoped BloCs 问题