等待 Future 构造对象的工厂

Factory waiting for a Future to construct the object

如何使 Factory 中的 Future 同步?我在这里向客户返回 null。

  factory Project.retrieve(String hash, CompetencesService service) {
    Project project;
    service.dbRef.child("project").once("value").then((snapshot) {
      Map val = snapshot.val();
      project = toObservable(new Project.fromJson(val));

      if(project != null) {
        project.listen(service);
        print("listening1");
      } else {
        project = toObservable(new Project.newHash(hash));
        service.dbRef.child("project").update(project.toJson()).then((error) {
          if(error) {
            //
          } else {
            project.listen(service);
            print("listening2");
          }
        });
      }
    });
    return project;
  }

有人 asked 解决了这个问题,但我正在寻找解决方法的示例。

目前无法创建异步构造函数或工厂,也无法同步等待 Future

后者有一个明显的原因:如果你将停止并同步等待依赖于当前隔离本身的东西(不是像文件i/o这样的外部事件),它永远不会发生,因为Isolate 处于一个停止在等待状态的单线程中。

所以,这里唯一的方法是让静态方法返回 FutureProject 实例,就像您提供的 link 中提到的那样:

static Future<Project> retrieve() async {
  var snapshot = await service.dbRef.child("project").once("value");
  Project project = toObservable(new Project.fromJson(snapshot.val()));

  ...

  return project; // Note you're actually returning a Future here
}

我尝试实现 Future 接口,然后工厂和构造函数可以 return 一个 Future 并且能够成为 await.

@proxy
class AsyncFact implements Future {
  factory AsyncFact() {
    return new AsyncFact._internal(new Future.delayed(
        const Duration(seconds: 1), () => '[Expensive Instance]'));
  }

  AsyncFact._internal(o) : _mirror = reflect(o);

  final InstanceMirror _mirror;
  @override
  noSuchMethod(Invocation invocation) => _mirror.delegate(invocation);
}

@proxy
class AsyncConst implements Future {
  AsyncConst() : _mirror = reflect(new Future.delayed(
          const Duration(seconds: 1), () => '[Expensive Instance]'));

  final InstanceMirror _mirror;
  @override
  noSuchMethod(Invocation invocation) => _mirror.delegate(invocation);
}

main() async {
  print(await new AsyncFact()); // [Expensive Instance]
  print(await new AsyncConst()); // [Expensive Instance]
}