flutter_flux
a dart app architecture library with uni-directional data flow inspired by refluxjs and facebook’s flux.
this is an experimental package and does not have official support from the flutter team. however, feedback is most welcome!
overview
flutter_flux
implements a uni-directional data flow pattern comprised of actions
, stores
, and storewatchers
.
it is based on w_flux, but modified to use flutter instead of react.
actions
initiate mutation of app data that resides instores
.- data mutations within
stores
trigger re-rendering of app view (defined instorewatcher
). - flutter
widgets
and other interaction sources dispatchactions
in response to user interaction. - and the cycle continues…
what’s included
action
an action
is a command that can be dispatched (with an optional data payload) and listened to.
in flutter_flux
, actions
are the sole driver of application state change. widgets and other objects dispatch actions
in response to
user interaction with the rendered view. stores
listen for these action
dispatches and mutate their internal data in
response, taking the action
payload into account as appropriate.
import 'package:flutter_flux/flutter_flux.dart';
// define an action
final action<string> displaystring = new action<string>();
// dispatch the action with a payload
displaystring('somepayload');
// listen for action dispatches
displaystring.listen(_displayalert);
_displayalert(string payload) {
print(payload);
}
bonus: actions
are await-able!
they return a future that completes after all registered action
listeners complete. it’s not generally recommended to
use this feature within normal app code, but it is quite useful in unit test code.
store
a store
is a repository and manager of app state. the base store
class provided by flutter_flux
should be extended to fit
the needs of your app and its data. app state may be spread across many independent stores depending on the complexity
of the app and your desired app architecture.
by convention, a store
‘s internal data cannot be mutated directly. instead, store
data is mutated internally in
response to action
dispatches. stores
should otherwise be considered read-only, publicly exposing relevant data only
via getter methods. this limited data access ensures that the integrity of the uni-directional data flow is maintained.
a store
can be listened to to receive external notification of its data mutations. whenever the data within a store
is mutated, the trigger
method is used to notify any registered listeners that updated data is available. in flutter_flux
,
storewatchers
listen to stores
, typically triggering re-rendering of ui elements based on the updated store
data.
import 'package:flutter_flux/flutter_flux.dart';
class randomcolorstore extends store {
// public data is only available via getter method
string _backgroundcolor = 'gray';
string get backgroundcolor => _backgroundcolor;
// actions relevant to the store are passed in during instantiation
randomcoloractions _actions;
randomcolorstore(randomcoloractions this._actions) {
// listen for relevant action dispatches
_actions.changebackgroundcolor.listen(_changebackgroundcolor);
}
_changebackgroundcolor(_) {
// action dispatches trigger internal data mutations
_backgroundcolor = '#' + (new random().nextdouble() * 16777215).floor().toradixstring(16);
// trigger to notify external listeners that new data is available
trigger();
}
}
bonus: stores
provide an optional terse syntax for action -> data mutation -> trigger operations.
// verbose syntax
actions.incrementcounter.listen(_handleaction);
_handleaction(payload) {
// perform data mutation
counter += payload;
trigger();
}
// equivalent terse syntax
triggeronaction(actions.incrementcounter, (payload) => counter += payload);
examples
simple examples of flutter_flux
usage can be found in the example
directory. the example readme
includes instructions for building / running them.
external consumption
flutter_flux
implements a uni-directional data flow within an isolated application or code module. if flutter_flux
is used as the
internal architecture of a library, this internal data flow should be considered when defining the external api.
- external api methods intended to mutate internal state should dispatch
actions
, just like any internal user interaction. - external api methods intended to query internal state should leverage the existing read-only
store
getter methods. - external api streams intended to notify the consumer about internal state changes should be dispatched from the
internalstores
, similar to theirtriggers
.
Comments are closed.