【问题标题】:Bloc plugin issue using BlocProvider使用 BlocProvider 的 Bloc 插件问题
【发布时间】:2020-11-27 12:20:48
【问题描述】:

我是 Flutter 和整个 Bloc 架构的新手,所以我正在关注教程以进一步了解它,但看起来 Bloc 以某种方式重新设计,因为我的版本具有完全不同的构造函数。

如果它的Bloc 有问题或者我做错了什么,我想知道。

我的 BlocProvider:

BlocProvider<PlacesBloc> buildBody(BuildContext context) {
    return BlocProvider(
      create: (_) => sl<PlacesBloc>(),
      child: Center(
        child: Padding(
          padding: const EdgeInsets.all(10),
          child: Column(
            children: <Widget>[
              // Top view
              SizedBox(height: 20),
              PlacesControls(),
              // Bottom view
              BlocBuilder<PlacesBloc, PlacesState>(
                // ignore: missing_return
                builder: (context, state) {
                  if (state is Empty) {
                    return MessageDisplay(
                      message: 'Search places around you!',
                    );
                  } else if (state is Loading) {
                    return LoadingWidget();
                  } else if (state is Loaded) {
                    return PlacesDisplay(places: state.places);
                  } else if (state is Error) {
                    return MessageDisplay(
                      message: state.message,
                    );
                  }
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

create 参数在我的 Bloc 版本中不存在。

也在我的Widget 中,我使用BlocProvider.of&lt;PlacesBloc&gt;(context).add(GetPlacesFromServer()); 将事件添加到我的“BlocProvider”,但我的 Bloc 中不存在这个.add 方法。

这是教程视频(底部)和我的(顶部)之间的构造函数差异:

【问题讨论】:

  • 您使用的是哪个包?名称和版本?
  • Felix Angelov 的 bloc,版本 5.0.0

标签: flutter dart bloc


【解决方案1】:

您的教程可能是为库的 4.0.0 版本编写的,它应该可以像您展示的那样工作。

在 5.0.0 和 6.0.0 版本中有很多 breaking changes

【讨论】:

    猜你喜欢
    • 2019-12-07
    • 2021-01-31
    • 2021-08-06
    • 2022-08-13
    • 1970-01-01
    • 2020-06-25
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多