Bloc: What should i return when an unknown state is passed to the builder callback?

Created on 20 Jun 2019  路  2Comments  路  Source: felangel/bloc

In a typical BlocBuilder implementation what should i return when an unknown state is passed?

BlocBuilder(
            bloc: _userBloc,
            builder: (BuildContext context, UserState state) {
              if (state is UserLoading) {
                return LoadingIndicator();
              }

              if (state is UserSuccess) {
                return LessonsPage(userBloc: _userBloc,);
              }
        })

The new master channel of flutter now the flutter analyse raise an issue if the callback have no return, what should i return in this case?

question

Most helpful comment

I don't even see how an unknown state might be passed since i'm the one defining them 馃ぃbut at static code analysis it makes sense to complain 馃槃

i like the ErrorWidget approach, because it mean that i forget to implement a UI for a possible state, so i want it to fail fast.

Thank you for your fast reply

All 2 comments

Hi @nerder 馃憢
Thanks for opening an issue and great question!

It's totally up to you, but a couple of options are:

  • use if/else if/else instead of just if
  • return some ErrorWidget which throws a FlutterError
  • return a Container()

Hope that helps 馃憤

I don't even see how an unknown state might be passed since i'm the one defining them 馃ぃbut at static code analysis it makes sense to complain 馃槃

i like the ErrorWidget approach, because it mean that i forget to implement a UI for a possible state, so i want it to fail fast.

Thank you for your fast reply

Was this page helpful?
0 / 5 - 0 ratings