mohammed nabil
mohammed nabil

Reputation: 495

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." 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

Upvotes: 1

Views: 7447

Answers (3)

Doan Bui
Doan Bui

Reputation: 4418

One thing that is really silly is that I used BlocListener instead of BlocBuilder, and it throws the above error, hope it can help someone.

Upvotes: 0

thisisyusub
thisisyusub

Reputation: 707

This happens because BlocBuilder requires return. 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.

And in new version of bloc, if you want to use BlocBuilder and BlocListener together, you can use BlocConsumer widget.

You can check it: BlocConsumer Example

Upvotes: 1

Federick Jonathan
Federick Jonathan

Reputation: 2864

That happens because you only specify one if condition for your state which is NotificationLoadedState. Your Bloc must have tried to build other state which is not specified therefore resulting BlocBuilder returning null.

For quick fix, you can just change your BlocBuilder to something like this for now.

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

Upvotes: 3

Related Questions