What pattern should I use in this example to load and process some data. As value
returns a value, it's not acceptable to have d
as a Future. How can I get the constructor to wait until load
has completed before continuing?
void main() {
var data = new Data(); // load data
print(data.value()); // data.d is still null
}
class Data {
String d;
Data() {
load();
}
Future<void> load() async {
d = await fn(); // some expensive function (e.g. loading a database)
}
String value() {
return d;
}
}