felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.79k stars 3.39k forks source link

Is type checking a good practice? #482

Closed durduman closed 5 years ago

durduman commented 5 years ago

I am using bloc architecture based this repo.

It seems that it is based a lot on type checking such as:

if (event is FooEvent) { 
 // ...
}
 //.. or
if (state is BarState) { 
 // ...
}

Is type checking a good practice? Are flutter & dart oriented towards this way coding or is this just a shortcut for this bloc implementation.

felangel commented 5 years ago

Hi @durduman 👋 Thanks for opening an issue!

Bloc does not have an opinion about how you model your events and states. In some cases, you can get away with using enums so you can simply switch on the event (see counter example).

In more complex cases, it's common to use classes to model your events and states. If you choose to go that route, you'll need a way to identify which type of event you are processing; type checking is one of the most easy/straightforward ways and there's nothing wrong with checking an object's type.

Hope that helps 👍

durduman commented 5 years ago

Thank you for your answer.