How can i implement one parent store with multiple other stores, and use some observables in parent store and use some observable in store2 for example?
PARENT STORE:
import 'package:mobx/mobx.dart';
import 'store1.dart';
import 'store2.dart';
class ParentStore = ParentStoreBase with _$ParentStore;
abstract class ParentStoreBase implements Store {
@observable
Store1 store1= new Store1();
@observable
Store1 store2= new Store2();
@observable
bool ParentLoading = false;
@action
setParentLoading (bool value) {
ParentLoading = value;
}
}
STORE 2: (_how to communicate between stores or instantiate ParentStore to get to ParentStore observables/values?_)
import 'package:mobx/mobx.dart';
import 'parent_store.dart';
class Store2= Store2Base with _$Store2;
abstract class Store2Base implements Store {
@observable
ParentStore parentStore= new ParentStore();
@observable
bool value2= false;
@action
setvalue2 (bool value) {
if(parentStore.ParentLoading == true)
value2 = value;
}
}
One quick option is to pass the ParentStore as constructor argument into your child stores
And how would passing ParentStore trough constructor look like in code? If you could give me an example?
And maybe you could add this too Examples for future users :)
Only showing the relevant parts:
// store2.dart
abstract class Store2Base implements Store {
Store2Base({this.parent}); // store the parent
final ParentStore parent;
@observable
bool value2= false;
@action
setvalue2 (bool value) {
if(parent.ParentLoading == true)
value2 = value;
}
}
// parent.dart
abstract class ParentStoreBase implements Store {
ParentStoreBase(){
this.store1 = Store1(parent: this); // pass the parent
}
@observable
Store1 store1;
@observable
bool ParentLoading = false;
@action
setParentLoading(bool value) {
ParentLoading = value;
}
}
Thanks for quick feedback, it works like charm!
@pavanpodila Doesn't that cause type errors? ParentStoreBase is not a ParentStore.
Most helpful comment
Only showing the relevant parts: