如何从内部函数/流侦听器中返回函数值?

Sky*_*827 1 indexeddb dart dart-async

当我发现我必须从内部函数中返回外部函数值时,我在dart中编写了一个从浏览器端索引数据库中删除对象的函数:

Future<bool> delete() {
    Transaction tx = db.transactionStore(storeName, "readwrite");
    ObjectStore os = tx.objectStore(storeName);
    os.delete(_key); // returns blank future, modifies tx

    // This is not correct, but shows the idea:
    if (tx.onComplete) {return true;}
    if (tx.onError) {return false;}
}
Run Code Online (Sandbox Code Playgroud)

此函数是我用于保存和加载到索引数据库的类的方法.当删除操作成功或失败时,我希望此函数返回truefalse包含相同的Future对象.但是,瓶颈是os.delete(_key);声明:它返回一个未来,但删除操作的实际成功或失败由tx.onComplete和提供tx.onError.这两个对象都是流,所以我需要创建处理来自它们的事件的匿名函数:

tx.onComplete.listen((e){
    return_to_outer_function(true);
});
tx.onError.listen((e){
    return_to_outer_function(false);
});
return_to_outer_function(bool) {
    return bool; // doesn't work
}
Run Code Online (Sandbox Code Playgroud)

如您所见,当我创建匿名函数时,return语句不再完成方法,而是内部函数.我可以让内部函数调用其他函数,但那些其他函数有自己的return语句,它们不会将结果返回给整个方法.

我尝试了设置临时变量并定期检查它们的方法,但这是一个非常不优雅的解决方案,我不想使用,不仅仅是潜在的错误,而是因为它会占用单线程事件循环.

是否可以从内部函数返回值到外部函数?或者是否有其他更好的方法从一组流中存在或不存在事件中获取值?或者是否有另一种方法可以使用IndexedDB来避免这个问题?

Gün*_*uer 5

你可以使用一个Completer.

Future<bool> delete() {
  Completer completer = new Completer();
  Transaction tx = db.transactionStore(storeName, "readwrite");
  ObjectStore os = tx.objectStore(storeName);

  tx.onError.first((e){
    //return_to_outer_function(false);
    completer.complete(false);
  });
  tx.onComplete.first(bool) {
    //return bool; // doesn't work
    completer.complete(true)
  }
  os.delete(_key); // register listeners and then do delete to be on the save side

  return completer.future;
}
Run Code Online (Sandbox Code Playgroud)

你然后称之为

delete().then((success) => print('succeeded: $success'));
Run Code Online (Sandbox Code Playgroud)

另见https://api.dartlang.org/apidocs/channels/be/dartdoc-viewer/dart:async.Completer