【问题标题】:Dart - Play a loop of FuturesDart - 玩一个循环的期货
【发布时间】:2017-11-04 20:30:42
【问题描述】:

我有这个功能,它使用audioplayer plugin 在 Flutter 内部播放声音。

play(int soundFrequency) async {
  final result = await audioPlayer.play("urltosound.wav");
}

它工作正常。但现在我希望能够连续播放多个声音。好像我把未来搞砸了。我尝试了这种方法,它非常肮脏和丑陋,但我只是想弄清楚:

playRandomSequence() async {
  final result = audioPlayer.play("urltosound.wav").then(play2(pickRandomSoundFrequency()));
}
play2(int soundFrequency) async {
  final result = audioPlayer.play("urltosound.wav");
}

基本上,一旦第一个 future 结束,我就用 .then() 方法调用下一个。

我从中得到的是这个错误:

type '_Future' is not a subtype of type '(dynamic) => dynamic' of 'f' where _Future is from dart:async

我该如何解决这个问题?

谢谢

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    .then() 的参数类型错误。试试:

    .then((_) => play2(pickRandomSoundFrequency()))
    

    您需要传递一个要调用的函数,而不是在构造then的参数时调用该函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 2020-01-30
      • 2015-10-16
      • 2020-05-09
      • 2020-10-17
      • 2014-08-06
      • 2019-07-05
      相关资源
      最近更新 更多