Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 如何完成未来?_Dart - Fatal编程技术网

Dart 如何完成未来?

Dart 如何完成未来?,dart,Dart,我正在搜索类似于以下内容的内容,其中我从方法返回一个对象,该对象可以等待,并从其原始函数完成: Future<dynamic> customFunction() { // The following class does not exist, but I am searching for a class that would fill that gap. FutureWithCompletion future = FutureWithCompletion();

我正在搜索类似于以下内容的内容,其中我从方法返回一个对象,该对象可以等待,并从其原始函数完成:

Future<dynamic> customFunction() {
    // The following class does not exist, but I am searching for a class that would fill that gap.
    FutureWithCompletion future = FutureWithCompletion();
    () async { // A callback that represents the usecase.
      // some computation
      future.completeWith('something');
    }
    return future;
}

/// This function accesses [customFunction].
/// It should print out "something" once the computation is done.
anotherFunction() async => print(await customFunction());
Future customFunction(){
//下面的类不存在,但我正在搜索一个可以填补这个空白的类。
FutureWithCompletion未来=FutureWithCompletion();
()async{//表示用例的回调。
//一些计算
将来。用(‘某物’)完成;
}
回归未来;
}
///此函数访问[customFunction]。
///一旦计算完成,它应该打印出“某物”。
另一个函数()异步=>打印(等待customFunction());
您需要使用:

Future方法(){
最终完成者=完成者();
计时器(持续时间(秒:5),()=>completer.complete('result');
返回completer.future;
}
您需要使用:

Future方法(){
最终完成者=完成者();
计时器(持续时间(秒:5),()=>completer.complete('result');
返回completer.future;
}
否则,请使用
完成符


否则就用一个
完成器

啊,你比我快。我想自己回答,但没关系。啊,你比我快。我想自己回答,但没关系。
Future<String> method() {
  final completer = Completer<String>();
  Timer(Duration(seconds: 5), () => completer.complete('result'));
  return completer.future;
}
return Future.value('something`);