我是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();
请帮助我:'(
答案 0 :(得分:3)
经过一段时间后,我希望this article将帮助您更多地了解context
。
通用解决方案与@Filled Stacks的答案相同。这意味着导航到新页面时必须pass the bloc,以便BlocProvider
可以找到将接收到哪种类型的Bloc。
我建议为整个应用程序初始化主要功能,该功能将初始化您的屏幕和整个屏幕的上下文。 例如:
class ListTodoPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider<TodoBloc>(
create: (context) => TodoBloc(TodoDao())..add(QueryTodoEvent()),
child: ListTodoPageful());
}
}
class ListTodoPageful extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return ListTodoState();
}
}
class ListTodoState extends State<ListTodoPageful> {
TodoBloc _todoBloc;
@override
Widget build(BuildContext context) {
_todoBloc = BlocProvider.of<TodoBloc>(
context);
//...
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
CupertinoPageRoute(
builder: (context) => BlocProvider.value(
value: _todoBloc, child: CreateTaskPage())));
}
//...
}
class _CreatePageState extends State<CreateTaskPage> {
TodoBloc _todoBloc;
@override
Widget build(BuildContext context) {
_todoBloc = BlocProvider.of<TodoBloc>(context);
}
在这里参考我的示例应用程序:https://github.com/huynguyennovem/flutter_todo
答案 1 :(得分:1)
下面的行是您的问题。该错误正是消息所显示的内容。
...
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