Lets take this dart class:
class Subject {
String a;
String b;
String c;
}
Now I want to use it trough a Proxy, to manage lazy loading and synchronization.
I want as well to have default values to use as placeholders while I'm loading the real data from the net. To keep thighs neat and isolated I added another class:
class Fallback implements Subject {
@override String a = 'a';
@override String b = 'b';
@override String c = 'c';
}
Now I have all the bricks I need to write down the "proxy with fallback" class:
class Proxy implements Subject {
Subject model;
Subject fallback = Fallback();
Future<void> slowlyPopulateModel() async => if (model == null) ... // do some async stuff that valorize model
@override
String get a {
slowlyPopulateModel();
return model?.a ?? fallback.a;
}
@override
set a(String _a) {
model?.a = a;
notifyListener();
}
// same getters and setters for b and c
}
By overriding get a
I can call the slow I/O method if needed and return the placeholder value of my Fallback
class. Once the new value is set my overridden set a(String _a)
will call notifyListener()
that will update my interface.
It is working fine, but I have manually override getter and setter for each field of my class (and they are many).
Does Dart have any trick to do this in a more DRY way?
E.g. some way to inject code to be executed before or after each getter or setter?