gpt4 book ai didi

flutter Bloc : No ancestor could be found starting from the context?

转载 作者:IT老高 更新时间:2023-10-28 12:41:49 26 4
gpt4 key购买 nike

我是 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 函数,它将通过屏幕初始化您的屏幕和上下文。例如:

  • 初始化和传递 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

关于 flutter Bloc : No ancestor could be found starting from the context?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55334005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com