mot 0.1.0
mot: ^0.1.0 copied to clipboard
A sober and efficient alternative to state management in Dart and Flutter. Subscribe and forget.
MOT #
Subscribe and forget. A state management framework for Dart that handles cleanup automatically.
Philosophy #
"The domain lives outside the widget tree. The UI observes it."
MOT prioritizes simplicity of interface over simplicity of implementation:
- Invisible — It just works. No boilerplate, no ceremony.
- Subscribe and forget — No manual cleanup. Ever.
- Zero surprises — Obvious, predictable behavior.
Installation #
dependencies:
mot: ^0.1.0
For Flutter projects, use mot_flutter instead.
Quick Start #
import 'package:mot/mot.dart';
// 1. Create a model
class Counter with Beacon {
final count = BeaconField<int>(0);
void increment() => count.value++;
}
// 2. Observe it
final counter = Counter();
counter.count.observe(myObserver, (self) => self.onCountChanged());
// 3. That's it. Cleanup is automatic.
The "self, not this" Pattern #
MOT uses a special pattern to prevent memory leaks:
// WRONG: Captures `this` → memory leak
model.observe(() => this.refresh());
// CORRECT: Receives `self` as parameter → safe
model.observe(this, (self) => self.refresh());
API #
Beacon (mixin) #
Transform any class into an observable:
class MyModel with Beacon {
void doSomething() {
// ... modify state ...
notify(); // Notify observers
}
}
| Method | Description |
|---|---|
observe(observer, callback) |
Register an observer |
removeObserver(observer) |
Unregister (optional) |
hasObserver(observer) |
Check if registered |
notify() |
Schedule notification |
notifySync() |
Immediate notification (testing) |
BeaconField #
An observable field that notifies on change:
final name = BeaconField<String>('');
name.value = 'Alice'; // Notifies observers
name.value = 'Alice'; // Same value → no notification
Key Behaviors #
Coalescing: Multiple changes = one notification
model.name.value = 'A'; // Schedules
model.name.value = 'B'; // Already scheduled
model.name.value = 'C'; // Already scheduled
// → ONE notification with value 'C'
Error isolation: One failing callback doesn't stop others.
Automatic cleanup: Observers are removed when garbage collected.
LLM Assistance #
If you're using an AI assistant, add docs/llm/mot_for_llms.md to your context for accurate help.
License #
MIT