vxstate
vxstate is a state management library built for flutter apps with focus on simplicity. it is inspired by storekeeper & libraries like redux, vuex etc with the power of streams. here is a basic idea of how it works:
- single store (single source of truth) to keep app’s data
- structured modifications to store with mutations
- widgets listen to mutations to rebuild themselves
- enhance this process with interceptors and effects
core of vxstate is based on the inheritedmodel widget from flutter.
getting started
add to your pubpsec:
dependencies:
...
vxstate: any
create a store:
import 'package:vxstate/vxstate.dart';
class mystore extends vxstore {
int count = 0;
}
define mutations:
class increment extends vxmutation<mystore> {
perform() => store.count++;
}
listen to mutations:
@override
widget build(buildcontext context) {
// define when this widget should re render
vxstate.listen(context, to: [increment]);
// get access to the store
mystore store = vxstate.store;
return text("${store.count}");
}
complete example:
import 'package:flutter/material.dart';
import 'package:vxstate/vxstate.dart';
// build store and make it part of app
void main() {
runapp(vxstate(
store: mystore(),
child: myapp(),
));
}
// store definition
class mystore extends vxstore {
int count = 0;
}
// mutations
class increment extends vxmutation<mystore> {
perform() => store.count++;
}
class myapp extends statelesswidget {
@override
widget build(buildcontext context) {
// define when this widget should re render
vxstate.listen(context, to: [increment]);
// get access to the store
mystore store = vxstate.store;
return materialapp(
home: scaffold(
body: column(
children: <widget>[
text("count: ${store.count}"),
raisedbutton(
child: text("increment"),
onpressed: () {
// invoke mutation
increment();
},
),
],
),
),
);
}
}
documentation
- vxstore – where your apps’s data is kept
- vxmutation – logic that modifies store
- vxbuilder, vxnotifier, vxconsumer – useful widgets for special cases
- vxeffect – chained mutations
- vxinterceptors – intercept execution of mutations
Comments are closed.