Bloc: support switch on bloc states

Created on 23 Jul 2019  路  1Comment  路  Source: felangel/bloc

I don't know if using runtimeTypeis a good approach for using switch for states

     switch (state.runtimeType) {
              case LoginStoredState:
                Navigator.of(context).pushReplacementNamed(PAGE_DASHBOARD);
                break;
              case LoginFailedState:
                showSnackBar((state as LoginFailedState).loginModel.message);
                break;
            }

            if (state is LoginStoredState) {
              Navigator.of(context).pushReplacementNamed(PAGE_DASHBOARD);
            } else if (state is LoginFailedState) {
              showSnackBar(state.loginModel.message);
            }
question

Most helpful comment

Hi @amreniouinnovent 馃憢
Thanks for opening an issue!

While it is definitely possible to switch on runtimeType I personally would not recommend it because runtimeType can be overridden so ideally you should not have logic that depends on runtimeType.

In simple cases, you can represent your state as an enum and switch on the enum. I'm currently trying to investigate several implementations of sealed classes in Dart and will hopefully be able to address this in the upcoming releases.

>All comments

Hi @amreniouinnovent 馃憢
Thanks for opening an issue!

While it is definitely possible to switch on runtimeType I personally would not recommend it because runtimeType can be overridden so ideally you should not have logic that depends on runtimeType.

In simple cases, you can represent your state as an enum and switch on the enum. I'm currently trying to investigate several implementations of sealed classes in Dart and will hopefully be able to address this in the upcoming releases.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timtraversy picture timtraversy  路  3Comments

MahdiPishguy picture MahdiPishguy  路  3Comments

nhwilly picture nhwilly  路  3Comments

Reidond picture Reidond  路  3Comments

nerder picture nerder  路  3Comments