【问题标题】:Flutter Bloc : There is no ancestor of my bloc in the context founded while I have it well provided with a BlocProvider.valueFlutter Bloc:在创建的上下文中没有我的 bloc 的祖先,而我很好地为其提供了 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)

标签: flutter dart bloc flutter-bloc


【解决方案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 的构建。

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 2020-04-29
    • 2021-02-08
    • 2021-12-13
    • 2019-06-28
    • 2020-03-26
    • 2020-08-21
    • 2021-08-20
    • 2019-08-22
    相关资源
    最近更新 更多