osam 6.3.8 osam: ^6.3.8 copied to clipboard
Lightweight and predictable state management. Allows you to save all application state by using Hve library
OSAM #
Osam is a Lightweight library to manage and notify the state.
The core element is IProperty
.
IProperty
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, IProperty
uses Equatable and storing
last known "good" hashcode. It allows you to use mutable data in the domain layer safe.
Library have a rule to make your code more readable and healthy. To change IProperty
's value
you should use PropertyChanger
. IProperty
s can be changed only inside class extending from
PropertyChanger
.
To start change IProperty
s you should use the method let
to define what you are changing and methods
set
or apply
to make actual change.
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 extends PropertyChanger {
final IProperty<int> value;
void increment() => let(value).apply((value) => value++);
Counter({IProperty<int> value}) : value = value ?? IProperty(0);
}
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