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

时间:2020-01-19 16:54:08

标签: flutter dart bloc flutter-bloc

我是flutter以及flutter_bloc的初学者,在这里我试图在blocbuilder中获取小部件,但得到“构建函数返回null。有问题的小部件是:BlocBuilder。构建函数绝不能返回null。”下面是bloc代码以及来自控制台的空错误。

        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: () {},
        ),])}

控制台日志错误

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

3 个答案:

答案 0 :(得分:2)

之所以会这样,是因为您只为状态指定了一个if条件,即NotificationLoadedState。您的Bloc必须尝试建立未指定的其他状态,因此导致BlocBuilder返回null。

要进行快速修复,您现在可以将BlocBuilder更改为类似的内容。

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

答案 1 :(得分:1)

发生这种情况是因为BlocBuilder需要返回。但是,您仅在 condition 为true时定义了收益。但是当条件为假时,它将返回null。这就是为什么会出现此错误的原因。因此,您必须添加 else 块或 return 语句。

在新版本的bloc中,如果要一起使用BlocBuilder和BlocListener,则可以使用 BlocConsumer 小部件。

您可以检查它: BlocConsumer Example

答案 2 :(得分:0)

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