Skip to main content

mobx-extension

Maintaining the State with MobXStore (MobX)#

A Store based on MobX is called MobXStore:

installation#

dependencies:  mobx_triple: any  flutter_mobx: any
class Counter extends MobXStore<Exception, int> {
    Counter() : super(0);
    Future<void> increment() async {        setLoading(true);
        await Future.delayed(Duration(seconds: 1));
        int value = state + 1;        if(value < 5) {            update(value);        } else {            setError(Exception('Error: state not can be > 4'));        }        setLoading(false);    }}

Our selectors (selectState, selectError, and selectBool) now will be Observable that can be listen separately using .observer() or in the Widget Tree with Observer both from flutter_mobx:


store.selectError.observer((_) => print(store.state));
...
Widget builder(BuildContext context){    return Observer(        builder: (_) => Text(store.state),    );}

For more information about the extension read the documentation for flutter_mobx

IMPORTANT: You can also continue to use the Triple (observer, ScopedBuilder and TripleBuilder);