osam 6.4.2 osam: ^6.4.2 copied to clipboard
Lightweight and predictable state management. Allows you to save all application state by using Hve library
import 'package:flutter/material.dart';
import 'package:osam/osam.dart';
part 'main.g.dart';
@HiveType(typeId: 0)
class Counter extends PropertyChanger {
@HiveField(0)
final IProperty<int> value;
void increment() => let(value).apply((value) => value++);
Counter({IProperty<int> value}) : value = value ?? IProperty(0);
}
final persist = Persist<Counter, Null>(appName: '123');
void main() async {
WidgetsFlutterBinding.ensureInitialized();
Hive.registerAdapter(CounterAdapter());
registerOsamAdapters();
await persist.init();
final counter = persist.appState ?? 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();
persist.save(state: counter, uiState: null, version: null);
},
),
body: Center(
child: ValueListenableBuilder(
valueListenable: counter.value,
builder: (_, value, __) => Text(
value.toString(),
style: const TextStyle(fontSize: 40),
),
),
),
),
);
}
}