SorryAnt
SorryAnt

Reputation: 33

Flutter Design pattern : Are there any top recommended?

I come from Android development background and I am new to Flutter and I have understood the basic hierarchy widget concepts and libraries of it. Just like how Android has design patterns like MVP , MVVM & MVI etc are there any popular design patterns for Flutter programming?

Upvotes: 1

Views: 2730

Answers (1)

Payam Asefi
Payam Asefi

Reputation: 2757

Flutter apps work with States and Streams and as I know there are 2 major patterns here, redux and BLoC.

Redux:

Redux is an Application State Management framework. In other words, its main objective is to manage a State. Redux is architectured around the following principles:

  1. Unidirectional data flow

  2. One Store

A Store acts like the orchestrator of Redux. The Store:

  • stores only one State
  • exposes one entry point, called dispatch which only accepts Actions in arguments
  • exposes one getter to fetch the current State
  • allows to (un-)register to be notified (via StreamSubscription) of any changes applied to the State
  • dispatches the actions and the store to the first MiddleWare
  • dispatches the actions and the current state to a Reducer (which might be a façade for several reducers)

Actions

Actions are the only types of input accepted by the Store access point. Actions, combined with the current State are used by the Middleware(s) and Reducer to process some function, which could lead to amending the State.

Actions only describe what happened

MiddleWare

A Middleware is a function commonly aimed at running asynchronously (but not necessarily), based on an Action. A Middleware simply uses a State (or an Action as a trigger) but does not change the State.

Reducers

A Reducer is normally a synchronous function which does some processing based on the combination Action - State. The outcome of the processing might lead to a new State. The Reducer is the only one allowed to change the State.

you can find out more about redux in here

BLoC

The BLoC pattern does not require any external library or package as it simply relies on the use of the Streams. However, for more friendly features (e.g. Subject), it is very often combined with the RxDart package.

The BLoC pattern relies on:

StreamController

A StreamController exposes a StreamSink to inject data in the Stream and a Stream to listen to data, flowing inside the Stream.

StreamBuilder

A StreamBuilder is a Widget which listens to a stream and rebuilds when new data is emitted by the Stream.

StreamSubscription

A StreamSubscription allows to listen to data being emitted by a stream and react.

BlocProvider

A BlocProvider is a convenient Widget, commonly used to hold a BLoC and make it available to descendant Widgets.

Here is an article about Flutter BLoc

Upvotes: 4

Related Questions