我的功能必须创建一个目录,然后将整个文件夹层次结构从另一个目录复制到这个新目录。所有操作都是异步完成的,但是我希望该函数返回一个Future,当我在其上调用.then(result)时,它将完成所有异步工作。

但是我不知道我应该在哪里放置completer.complete()来实现这一目标。



Future<Directory> createCopyDirectory(Directory directoryToCreate){
  Completer<Directory> completer = new Completer<Directory>();
  completer.complete(
      directoryToCreate.create().then((directory){
        Directory contentToCopy = new Directory(globalPathOfDirectoryToCopy);
        List<Future> creatingContent = new List<Future>();
        contentToCopy.list(recursive:true, followLinks:false).forEach((f){
          if (f is File){
            File fileToCreate = new File(f.path.replaceFirst('pages', userID));
            creatingContent.add(fileToCreate.create(recursive:true).then((_){
              f.readAsString().then((fileContent){
                fileToCreate.writeAsString(fileContent);
              });
            }));
          }
        });
        return Future.wait(creatingContent).then((_){ return directoryToCreate;});
    })
  );
  return completer.future;
}

我精确地说,我的函数可以按预期工作,但是,如果我尝试直接访问应在该函数中创建的内容(如then()调用中的内容),Dart给我带来了期望,就像我没有创建内容一样。因此,completer.complete()肯定放错位置,并在创建内容之前调用then()

我尝试在结尾completer.complete()上使用Future.wait(creatingContent)或通过用return directoryToCreate替换completer.complete(directoryToCreate)来进行尝试,但是结果是相同的。

对于在这种情况下构建适当的基于Future的功能的方式,我有些困惑。

最佳答案

您不需要在这里输入Completer

Future<Directory> createCopyDirectory(Directory directoryToCreate) {
  return directoryToCreate.create().then((directory) {
    String userID = split(userDirectory.path).last;
    Directory contentToCopy = new Directory(globalPathOfDirectoryToCopy);
    List<Future> creatingContent = new List<Future>();
    return contentToCopy
        .list(recursive: true, followLinks: false)
        .forEach((File f) {
      if (f is File) {
        File fileToCreate = new File(f.path.replaceFirst('pages', userID));
        creatingContent.add(fileToCreate.create(recursive: true).then((_) {
          return f.readAsString().then((fileContent) {
            return fileToCreate.writeAsString(fileContent);
          });
        }));
      }
    }).then((_) {
      return Future.wait(creatingContent).then((_) {
        return directoryToCreate;
      });
    });
  });
}

只是为了演示如何使用Completer:

Future<Directory> createCopyDirectory(Directory directoryToCreate) {
  Completer<Directory> completer = new Completer<Directory>();
  directoryToCreate.create().then((directory) {
    String userID = split(userDirectory.path).last;
    Directory contentToCopy = new Directory(globalPathOfDirectoryToCopy);
    List<Future> creatingContent = new List<Future>();
    contentToCopy.list(recursive: true, followLinks: false).forEach((f) {
      if (f is File) {
        File fileToCreate = new File(f.path.replaceFirst('pages', userID));
        creatingContent.add(fileToCreate.create(recursive: true).then((_) {
          return f.readAsString().then((fileContent) {
            return fileToCreate.writeAsString(fileContent);
          });
        }));
      }
    }).then((_) => Future
        .wait(creatingContent)
        .then((_) => completer.complete(directoryToCreate)));
  });
  return completer.future;
}

关于dart - 我应该在这一系列的Future函数中将completer.complete()放在哪里?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28677715/

10-09 12:26