Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.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
Flutter 异步任务在WorkManager CallbackDispatcher中不工作_Flutter_Asynchronous_Flutter Plugin - Fatal编程技术网

Flutter 异步任务在WorkManager CallbackDispatcher中不工作

Flutter 异步任务在WorkManager CallbackDispatcher中不工作,flutter,asynchronous,flutter-plugin,Flutter,Asynchronous,Flutter Plugin,我正在使用WorkManager进行后台服务。我的代码如下 void callbackDispatcher() { Workmanager.executeTask((task, inputData) async { switch (task) { case "uploadLocalData": print("this method was called from background!"); awai

我正在使用WorkManager进行后台服务。我的代码如下

void callbackDispatcher() {
  Workmanager.executeTask((task, inputData) async {
    switch (task) {
      case "uploadLocalData":
        print("this method was called from background!");
        await BackgroundProcessHandler().uploadLocalData();
        print("background Executed!");
        return true;
       
        break;
      case Workmanager.iOSBackgroundTask:
        print("iOS background fetch delegate ran");
        return true;
        break;
    }
    return false;
   
  });
}

有没有办法在executeTask中等待async方法?

异步化非异步内容的方法是通过完成符。你创建了一个完成者,等待它的未来,并在未来完成它

Completer uploadCompleter = Completer();

void callbackDispatcher() {
  Workmanager.executeTask((task, inputData) async {
    switch (task) {
      case "uploadLocalData":
        print("this method was called from background!");
        await BackgroundProcessHandler().uploadLocalData();
        print("background Executed!");
        uploadCompleter.complete();
        return true;
       
        break;
      case Workmanager.iOSBackgroundTask:
        print("iOS background fetch delegate ran");
        return true;
        break;
    }
    return false;
   
  });
}

// somewhere else
await uploadCompleter.future;


刚刚添加了
等待上传完成器.future上传到
uploadCompleter.complete(),这对我来说非常有效。谢谢@gazihanalankus我很高兴它能帮上忙,但我不明白:)如果你
等待uploadCompleter.future之前
uploadCompleter.complete(),它将永远无法到达
uploadCompleter.complete(),对吗?我也很震惊,bcoz概念上是错的,。。但这对我很管用。