【问题标题】:Flutter Bloc : No ancestor could be found starting from the context?Flutter Bloc:从上下文中找不到祖先?
【发布时间】:2023-03-09 10:28:02
【问题描述】:

我是 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();

请帮帮我:'(

【问题讨论】:

    标签: flutter


    【解决方案1】:

    在遇到这个问题一段时间后,我希望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

    【讨论】:

      【解决方案2】:

      下面这行是你的问题。错误正是消息所说的。

      ...
      child: BlocBuilder<BtnEvent, BtnState>(
           bloc: BlocProvider.of<BtnBloc>(context), <------- Problem line
           builder: (context, BtnState state) {
      ...
      

      您在那里使用的上下文没有附加 BlocBuilder,因此没有任何东西将 BloC 从顶部向下传递到您的 LoginState。你可以

      1. 使用您现有的 bloc (_btnBloc) 并将其传入(推荐
      2. 使用 BlocProvider 包装将 LoginState 显示在屏幕上的小部件,以便您可以在小部件状态下访问它。

      如果这是您的第一个视图,请使用 1。

      改变

       bloc: BlocProvider.of<BtnBloc>(context), <------- Problem line
      

      bloc: _btnBloc
      

      【讨论】:

      • @VincentGuillois 很高兴能提供帮助。
      猜你喜欢
      • 2021-03-03
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      相关资源
      最近更新 更多