【发布时间】: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);
这是我点击浮动按钮时的错误:
════════ 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