Suragch
Sep 13, 2021

--

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.

--

--

Suragch
Suragch

Written by Suragch

Flutter and Dart developer. Twitter: @suragch1, Email: suragch@suragch.dev

No responses yet