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
这是我创建 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 页面脚手架底部 sheet :
bottomSheet: BlocProvider.value(
value: BlocProvider.of<NavigateHomeScreenBloc>(context),
child: AddCollaboratorButton(),
),
我的浮动按钮将我的页面推送到祖先发生错误的地方(我的 BlocProvider.value 在这里 -> AddCollaboratorButton class):
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
...
════════════════════════════════════════════════════════════════════════════════
好的,我终于找到了解决方案,我在 Navigator.of(context).push 之前定义了我的集团:
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 的构建。
这是我创建 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 页面脚手架底部 sheet :
bottomSheet: BlocProvider.value(
value: BlocProvider.of<NavigateHomeScreenBloc>(context),
child: AddCollaboratorButton(),
),
我的浮动按钮将我的页面推送到祖先发生错误的地方(我的 BlocProvider.value 在这里 -> AddCollaboratorButton class):
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
...
════════════════════════════════════════════════════════════════════════════════
好的,我终于找到了解决方案,我在 Navigator.of(context).push 之前定义了我的集团:
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 的构建。