Flutter Design Patterns: 6 — State

An overview of the State design pattern and its implementation in Dart and Flutter

Previously in the series, I have analysed one of the most practical design patterns you can use in day-to-day coding — Strategy. In this article, I will analyse and implement a pattern, which has a similar structure to Strategy but is used for a different purpose — the State design pattern.

Table of Contents

  • Analysis
  • Implementation
  • Other articles in this series
  • Your contribution

What is the State design pattern?

Different states of the water (source)

The State is a behavioural design pattern, which intention in the GoF book is described like this:

Allow an object to alter its behaviour when its internal state changes. The object will appear to change its class.

To understand a general idea behind the State design pattern, you should be familiar with the concept of a Finite-state machine. Here you can see an example of a Finite-state machine model of a JIRA task:

Different states of the task (source)

At any given moment, there is a finite number of states which a task can be in. Each one of the states is unique and acts differently. At any time, the task could be switched from one state to another. The only limitation — there is a finite set of switching rules (transitions) which define the states that could be switched to from the current state. That is, a task from a state of open could not be switched to reopened, closed could not be switched to in progress or resolved, etc.

The similar approach could be applied in OOP to objects. To simply implement the Finite-state machine in code is by using several conditional operators in a single class and selecting the appropriate behaviour depending on the current state of the object. However, by introducing new states, this kind of code becomes very difficult to maintain, states interlace with each other, the class is committed to a particular state-specific behaviour. I have not even mentioned how difficult it becomes to test this kind of code and the implemented business logic inside.

To resolve these issues, a State design pattern is a great option since for each state a separate State class is created which encapsulates the logic of the state and changes the behaviour of a program or its context. Also, it makes adding new states an easy task, state transitions becomes explicit.

Let’s move to the analysis to understand how the State pattern works and how it could be implemented.


Structure of the State design pattern (source)
  • Context — maintains an instance of a ConreteState subclass that defines the current state. The Context class does not know any details about ConcreteStates and communicates with the state object via the State interface. Also, Context provides a setter method to change the current state from ConcreteStates classes;
  • State — defines an interface which encapsulates the state-specific behaviour and methods. These methods should make sense for all ConreteStates, there should be no methods defined which will never be called from a specific implementation of the State interface;
  • ConcreteStates — each class implements a behaviour associated with a state of the Context. State objects may reference the Context to obtain any required information from the context or to perform the state transition by replacing the state object linked to the Context class (via the exposed setter method);
  • Client — uses the Context object to reference the current state and initiate its transition, also it may set the initial state for the Context class if needed.

Strategy vs State



The following implementation (or at least the idea behind it) could be applied to pretty much every Flutter application which loads resources from any kind of external source, e.g., API. For instance, when you load resources asynchronously using HTTP and calling the REST API, usually it takes some time to finish the request. How to handle this and not “freeze” the application while the request is finished? What if some kind of error occurs during this request? A simple approach is to use animations, loading/error screens, etc. It could become cumbersome when you need to implement the same logic for different kind of resources. For this, the State design pattern could help. First of all, you clarify the states which are common for all of your resources:

  • Empty — there are no results;
  • Loading — the request to load the resources is in progress;
  • Loaded — resources are loaded from the external source;
  • Error — an error occurred while loading the resources.

For all of these states, a common state interface and context is defined which could be used in the application.

Let’s dive into the implementation details of the State design pattern and its example in Flutter!

Class diagram

Class Diagram — Implementation of the State design pattern

IState defines a common interface for all the specific states:

  • nextState() — changes the current state in StateContext object to the next state;
  • render() — renders the UI of a specific state.

NoResultsState, ErrorState, LoadingState and LoadedState are concrete implementations of the IState interface. Each of the states defines its representational UI component via render() method, also uses a specific state (or states, if the next state is chosen from several possible options based on the context) of type IState in nextState(), which will be changed by calling the nextState() method. In addition to this, LoadedState contains a list of names, which is injected using the state’s constructor, and LoadingState uses the FakeApi to retrieve a list of randomly generated names.

StateContext saves the current state of type IState in private currentState property, defines several methods:

  • setState() — changes the current state;
  • nextState() — triggers the nextState() method on the current state;
  • dispose() — safely closes the stateStream stream.

The current state is exposed to the UI by using the outState stream.

StateExample widget contains the StateContext object to track and trigger state changes, also uses the NoResultsState as the initial state for the example.



Specific implementations of the IState interface

LoadedState implements the specific state which is used when resources are loaded from the API without an error and the result widget should be provided to the screen.

NoResultsState implements the specific state which is used when a list of resources is loaded from the API without an error, but the list is empty. Also, this state is used initially in the StateExample widget.

LoadingState implements the specific state which is used on resources loading from the FakeApi. Also, based on the loaded result, the next state is set in nextState() method.



StateExample implements the example widget of the State design pattern. It contains the StateContext, subscribes to the current state’s stream outState and provides an appropriate UI widget by executing the state’s render() method. The current state could be changed by triggering the changeState() method (pressing the Load names button in UI).

StateExample widget is only aware of the initial state class — NoResultsState but does not know any details about other possible states, since their handling is defined in StateContext class. This allows to separate business logic from the representational code, add new states of type IState to the application without applying any changes to the UI components.

The final result of the State design pattern’s implementation looks like this:

As you can see in the example, the current state is changed by using a single Load names button, states by themselves are aware of other states and set the next state in the StateContext.

All of the code changes for the State design pattern and its example implementation could be found here.

Your contribution

Flutter Community

Articles and Stories from the Flutter Community

Mangirdas Kazlauskas

Written by

Software Engineer | Flutter Enthusiast https://www.linkedin.com/in/mangirdas-kazlauskas/

Flutter Community

Articles and Stories from the Flutter Community

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade