osam 6.2.5 osam: ^6.2.5 copied to clipboard
Lightweight and predictable state management. Allows you to save all application state by using Hive library
OSAM #
Osam is a Lightweight library to manage and notify the state.
The core element is PropertyNotifier
.
PropertyNotifier
allows you to change a value and notify everything listening to it.
Same as ValueNotifier
you can add listeners and remove listeners.
To decide should listeners get a notification, PropertyNotifier
uses Equatable and storing
last known "good" hashcode. It allows you to use mutable data in the domain layer safe.
PropertyNotifier
implements IProperty to prevent the setting of new value from the presentation layer.
IProperty
very actively uses in osam_flutter and good
fits with it.
Example:
import 'package:flutter/material.dart';
import 'package:osam/osam.dart';
class Counter {
// good constructor structure for restoring entity from hive
Counter([this._value]) {
_value ??= PropertyNotifier(0);
}
PropertyNotifier<int> _value;
IProperty<int> get value => _value;
void increment() => _value.value++;
}
void main() {
final counter = Counter();
runApp(Example(counter: counter));
}
class Example extends StatelessWidget {
final Counter counter;
const Example({Key key, this.counter}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => counter.increment(),
),
body: Center(
child: ValueListenableBuilder(
valueListenable: counter.value,
builder: (_, value, __) => Text(
value.toString(),
style: const TextStyle(fontSize: 40),
),
),
),
),
);
}
}
Also, you can use the Persist repository to store your AppState and UIState. osam_flutter uses a persist repository to store all-state.
If you prefer to follow clean architecture principles, I recommend you to write use-case to change
the state of the application. Usecase could be easily provided by UseCaseContainer