【问题标题】:Can I call a non-async function asynchronously in Dart?我可以在 Dart 中异步调用非异步函数吗?
【发布时间】:2014-06-14 18:21:18
【问题描述】:

我想使用 DecodeGifAnimation 解码带有the image package 的 gif,但它需要的时间太长,导致我的 web 应用程序冻结。该库似乎也没有任何异步方法。我查看了如何在 Dart 中进行异步处理,似乎我需要使用 Futures,虽然我不确定如何为我的函数创建一个。

不太清楚我在做什么

void decode(Uint8List data) {
  Future anim = decodeGifAnimation(data); // but it returns Animation, not a Future!
  anim.then(prepare);
}

【问题讨论】:

    标签: dart dart-async


    【解决方案1】:
    void decode(Uint8List data) {
      new Future(() => decodeGifAnimation(data)).then(prepeare);
    }
    

    Future decode(Uint8List data) {
      return new Future(() => decodeGifAnimation(data)).then(prepeare);
    }
    

    如果你想在方法返回调用方法时做一些异步处理,比如

    decode(data).then(xxx);
    

    【讨论】:

      猜你喜欢
      • 2019-08-18
      • 2020-03-09
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 2020-12-26
      • 2020-04-12
      • 2017-08-13
      • 1970-01-01
      相关资源
      最近更新 更多