Last updated:
0 purchases
reactive state db
Reactive state db #
Description #
This package can be used to manage the state like a database.
Example usage #
import 'package:reactive_state_db/schema.dart';
import 'dart:async';
class Person {
String name;
int age;
bool loading = false;
Person(this.name, this.age, bool? loading) {
this.loading = loading ?? this.loading;
}
@override
String toString() {
return '$name $age $loading';
}
Person copyWith({String? name, int? age, bool? loading}) {
return Person(
name ?? this.name,
age ?? this.age,
loading ?? this.loading,
);
}
}
Future<void> waitThreeSeconds() async {
await Future.delayed(Duration(seconds: 3));
}
void start() async {
final personStore = SchemaClass<Person>();
personStore.add(Person('Alice', 25, false));
personStore.get((person) => person.name == 'Alice').listen((person) {
print(
'1: Name: ${person?.name} | Age: ${person?.age} | loading: ${person?.loading}');
});
personStore.get((person) => person.age >= 30).listen((person) {
print(
'2: Name: ${person?.name} | Age: ${person?.age} | loading: ${person?.loading}');
});
personStore.setBeforeUpdate((newValue, setIntermediateState) async {
setIntermediateState(newValue.copyWith(loading: true));
await waitThreeSeconds();
});
personStore.setBeforeAdd((newValue) async {
await waitThreeSeconds();
});
personStore.setBeforeRemove((newValue) async {
print("Removing following: $newValue");
await waitThreeSeconds();
print("Removed following: $newValue");
});
waitThreeSeconds().then((_) {
personStore.update(
(person) => person.name == 'Alice', Person('Alice', 26, false));
waitThreeSeconds().then((_) {
personStore.update(
(person) => person.name == 'Alice', Person('Alice', 27, false));
waitThreeSeconds().then((_) {
personStore.update(
(person) => person.name == 'Bob', Person('Bob', 31, false));
waitThreeSeconds().then(
(_) => {personStore.remove((person) => person.name == 'Alice')});
});
});
});
personStore.add(Person('Bob', 30, false));
}
void main(List<String> arguments) async {
start();
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.