Skip to content

Instantly share code, notes, and snippets.

@bsutton
Created October 31, 2019 21:55
Show Gist options
  • Save bsutton/369e71bb173ba3c19d28f6d6fec2072a to your computer and use it in GitHub Desktop.
Save bsutton/369e71bb173ba3c19d28f6d6fec2072a to your computer and use it in GitHub Desktop.
void main() async {
// pretend I've just return the set of options from an REST call to the db
List<RefOption> refs = [RefOption(2), RefOption(3), RefOption(1)];
await resolveList(refs);
refs.sort((lhs, rhs) => lhs.option.ordinal - rhs.option.ordinal);
for (RefOption ref in refs)
{
print("option: ${ref.option.ordinal}");
}
}
class RefOption
{
Option _option;
int _option_id;
RefOption(this._option_id);
void resolve() async {
await Future.delayed(Duration(seconds: 2), () => _option = Option(_option_id, _option_id));
}
Option get option {
if (_option == null)
throw "Not resolved";
return _option;
}
}
class Option
{
int _id; // set by the database
int _ordinal;
Option(this._id, this._ordinal);
int get ordinal
{
return _ordinal;
}
}
void resolveList(List<RefOption> refs) async {
for (RefOption ref in refs) {
await ref.resolve();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment