我是 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)中更改一些内容,但没有任何效果。
这是我的博客课:
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();
请帮帮我:'(
最佳答案
在遇到这个问题一段时间后,我希望this article将帮助您更多地了解context。
一般解决方案与@Filled Stacks 的答案相同。这意味着您必须 pass the bloc导航到新页面时,BlocProvider 可以找到您将收到的 Bloc 类型。
我建议为整个应用程序初始化 main 函数,它将通过屏幕初始化您的屏幕和上下文。 例如:
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
关于 flutter Bloc : No ancestor could be found starting from the context?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55334005/
BLOC说明bloc是一个可预测的状态管理库,有助于实现BLoC设计模式。简单和轻便,高度可测试,适用于Dart、Flutter和AngularDart。简单使用声明自定义bloc类,继承于Bloc,然后添加相应的事件对象和状态的处理(通过emit把新的状态反馈出去),如下:///APP全局Bloc类classAppGlobalBlocextendsBloc{AppGlobalBloc(super.initialState){//下面在注册相应的事件,并在事件接收后变更状态on((event,emit)=>emit(state.copyWith(themeColor:event.themeCo
BLOC说明bloc是一个可预测的状态管理库,有助于实现BLoC设计模式。简单和轻便,高度可测试,适用于Dart、Flutter和AngularDart。简单使用声明自定义bloc类,继承于Bloc,然后添加相应的事件对象和状态的处理(通过emit把新的状态反馈出去),如下:///APP全局Bloc类classAppGlobalBlocextendsBloc{AppGlobalBloc(super.initialState){//下面在注册相应的事件,并在事件接收后变更状态on((event,emit)=>emit(state.copyWith(themeColor:event.themeCo