I like how the Bloc library does this. (example)
You define an abstract base class like PizzaState
and then extend it for each state: PizzaInitial
, PizzaLoadInProgress
, PizzaLoadSuccess
, and PizzaLoadFailure
. For the failure state you can extend it with an error message property. Then in your build
method you can check the state type using the is
keyword and access the error message if the state is PizzaLoadFailure
.
This is all just standard Dart. There is no dependency on the Bloc library to do it this way.