简体   繁体   English

一个构建函数返回null,有问题的小部件是:BlocBuilder<NotificationBloc, NotificationState> 在 flutter_bloc 包中

[英]A build function returned null,The offending widget is: BlocBuilder<NotificationBloc, NotificationState> in flutter_bloc package

I am a beginner in flutter as well as in flutter_bloc where i'm trying to get the widget in blocbuilder but getting the "A build function returned null. The offending widget is:BlocBuilder. Build functions must never return null."我是 flutter 以及 flutter_bloc 的初学者,我试图在 blocbuilder 中获取小部件,但得到“构建函数返回空值。有问题的小部件是:BlocBuilder。构建函数绝不能返回空值。” Below is the bloc code and also the null error which is from console.下面是块代码以及来自控制台的空错误。

        Column(
          children: <Widget>[
            Container(
              color: Colors.white,
              child: BlocListener<NotificationBloc, NotificationState>(
                listener: (context, state) {
                },
                child: BlocBuilder<NotificationBloc, NotificationState>(
                  builder: (context, state) {
                    if (state is NotificationLoadedState) {
                      return NotificationIconBuild();
                    }
                  },
                ),
              ),
            )
          ],
        )
  Widget NotificationIconBuild() {
    return Column(
      children: <Widget>[
        IconButton(
          icon: Icon(Icons.notifications),
          onPressed: () {},
        ),
        IconButton(
          icon: Icon(Icons.notifications),
          onPressed: () {},
        ),])}

Error from console log控制台日志中的错误

I/flutter (13632): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (13632): The following assertion was thrown building BlocBuilder<NotificationBloc, NotificationState>(dirty,
I/flutter (13632): state: _BlocBuilderBaseState<NotificationBloc, NotificationState>#7fa62):
I/flutter (13632): A build function returned null.
I/flutter (13632): The offending widget is:
I/flutter (13632):   BlocBuilder<NotificationBloc, NotificationState>
I/flutter (13632): Build functions must never return null.
I/flutter (13632): To return an empty space that causes the building widget to fill available room, return
I/flutter (13632): "Container()". To return an empty space that takes as little room as possible, return
I/flutter (13632): "Container(width: 0.0, height: 0.0)".
I/flutter (13632): 
I/flutter (13632): The relevant error-causing widget was:
I/flutter (13632):   BlocBuilder<NotificationBloc, NotificationState>
I/flutter (13632):   file:///C:/Users/Nabil/AndroidStudioProjects/flutter_save/lib/home_page.dart:77:24

That happens because you only specify one if condition for your state which is NotificationLoadedState .发生这种情况是因为您只为您的状态指定一个if条件,即NotificationLoadedState Your Bloc must have tried to build other state which is not specified therefore resulting BlocBuilder returning null.您的 Bloc 必须尝试构建未指定的其他状态,因此导致BlocBuilder返回 null。

For quick fix, you can just change your BlocBuilder to something like this for now.为了快速修复,您现在可以将BlocBuilder更改为类似的内容。

                child: BlocBuilder<NotificationBloc, NotificationState>(
                  builder: (context, state) {
                    if (state is NotificationLoadedState) {
                      return NotificationIconBuild();
                    } else {
                      return Container();,
                    }
                  },
                ),

This happens because BlocBuilder requires return.这是因为 BlocBuilder 需要返回。 But you defined only a return when condition is true.但是您仅在条件为真时定义了返回。 But when the condition is false, it will return null.但是当条件为假时,它将返回空值。 That is why it gives this error.这就是它给出这个错误的原因。 So you must add else block or return statement.所以你必须添加else块或return语句。

And in new version of bloc, if you want to use BlocBuilder and BlocListener together, you can use BlocConsumer widget.而在新版本的 bloc 中,如果你想同时使用 BlocBuilder 和 BlocListener,你可以使用BlocConsumer小部件。

You can check it: BlocConsumer Example你可以检查一下: BlocConsumer Example

一件非常愚蠢的事情是我使用BlocListener而不是BlocBuilder ,它抛出了上述错误,希望它可以帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 flutter_bloc:构建 function 返回 null - flutter_bloc: Build function returned null flutter_bloc many Event to many BlocBuilder - flutter_bloc many Event to many BlocBuilder flutter_bloc的BlocBuilder是否可以避免重建未更改的Widget部分? - Can BlocBuilder of flutter_bloc avoid rebuild part of Widget which not changing? 如果您在 BlocBuilder 中指定 bloc,flutter_bloc 会处理 cubit 吗? - Does flutter_bloc dispose of the cubit if you specify the bloc in the BlocBuilder? 生成函数返回null令人讨厌的窗口小部件是:StreamBuilder <Response> - A build function returned null The offending widget is: StreamBuilder<Response> 构建函数返回 null。 有问题的小部件是:StoryCamera - A build function returned null. The offending widget is: StoryCamera 构建 function 返回 null,有问题的小部件是:StreamBuilder<querysnapshot></querysnapshot> - A build function returned null,The offending widget is: StreamBuilder<QuerySnapshot> flutter_bloc-如果没有BlocBuilder,如何获得价值? - flutter_bloc - how can i get value without BlocBuilder? flutter_bloc BlocBuilder 没有重建,也没有在 BlocObserver 中观察到状态变化 - flutter_bloc BlocBuilder not rebuilding as well state change not observed in BlocObserver 使用 flutter_bloc 为 Flutter App 构建架构 - Build the architecture for a Flutter App with flutter_bloc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM