让我们学习这种飞镖课程:
class Subject {
String a;
String b;
String c;
}
现在,我想通过代理使用它来管理延迟加载和同步。
我还希望在从网络加载真实数据时使用默认值作为占位符。为了使大腿保持整洁和孤立,我添加了另一类:
class Fallback implements Subject {
@override String a = 'a';
@override String b = 'b';
@override String c = 'c';
}
现在我拥有写下“具有后备代理”类的所有必要知识:
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
}
通过重写get a
,如果需要,我可以调用慢速I / O方法,并返回我的Fallback
类的占位符值。设置新值后,覆盖的set a(String _a)
将调用notifyListener()
,这将更新我的界面。它工作正常,但是我已经为类的每个字段手动覆盖了getter和setter(而且它们很多)。
Dart有技巧以更DRY的方式做到这一点吗?例如。一种在每个getter或setter之前或之后注入要执行的代码的方法?
我建议为此查看Streams
。
此代码示例将返回一个初始值,获取一个新值,并通过流将其通知给侦听器。
import 'dart:async';
class Subject {
// Current value, initially at "a"
String _a = "a";
StreamController<String> _aController = StreamController<String>();
String get a {
_updateA();
return _a;
}
Stream<String> get aStream => _aController.stream;
void _updateA() async {
String newValue = await _fetchA();
_a = newValue; // update the current value
_aController.add(newValue); // push the new value onto the stream for listeners
}
// return a String after two seconds
Future<String> _fetchA() async {
return Future.delayed(
Duration(seconds: 2),
() => "New value for _a",
);
}
// This closes the stream
void dispose() {
_aController.close();
}
}
main(args) {
final subject = Subject();
// listen to changes
subject.aStream.listen((value) {
print("new value of a: $value");
});
// get current value
final currentValue = subject.a;
print("current value of a: $currentValue");
}
此示例的输出
a的当前值:a
((两秒钟后)a的新值:_a的新值
与StreamBuilder
一起在Flutter中使用
StreamBuilder<String>(
stream: subject.aStream,
initialData: subject.a,
builder: (context, snapshot) {
final valueOfA = snapshot.data;
return Text("value is $valueOfA");
}
)
某些样板代码可以用BehaviorSubject中的RxDart代替。但这需要将另一个依赖项导入到项目中。