我是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。
更改
bloc: BlocProvider.of<BtnBloc>(context), <------- Problem line
至
bloc: _btnBloc
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句