import 'store.dart'; /// A [Store] that has a [registerStores] /// method which connects these child stores to this [RootStore], /// initializes them and then disposes of the subscription. abstract class RootStore extends Store { List _children = []; /// Register stores with this [RootStore]. /// /// The root store will subscribe to all childrens /// [notifyListeners] calls, initialize them, and dispose /// of the subscription. void registerChildren(List children) { _children = children; } @override Future initState() async { if (_children.isEmpty) print( "WARNING: $this has no _children. Typically a RootStore would have _children stores.", ); /// Connect the root store's [notifyListeners] method to /// all child stores [notifyListeners] method. _children.forEach((e) => e.addListener(notifyListeners)); /// Await all child store's [initState] await Future.wait(_children.map((e) => e.initState())); return super.initState(); } @override void dispose() { /// Dispose all [notifyListeners] handlers. _children.forEach((e) { e.removeListener(notifyListeners); e.dispose(); }); super.dispose(); } }