OhMad
OhMad

Reputation: 7289

Dart - Play a loop of Futures

I have this function which plays a sound inside of Flutter using the audioplayer plugin.

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

It works fine. But now I want to be able to play multiple sounds in a row. It seems as if I mess up with the futures. I tried this approach, which is very dirty and ugly but I was just trying to figure things out:

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

Basically, as soon as the first future is over, I call the next one with the .then() method.

What I get from it is this error:

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

How can I fix this?

Thanks

Upvotes: 0

Views: 622

Answers (1)

Randal Schwartz
Randal Schwartz

Reputation: 44081

You have the wrong type for the argument of .then(). Try:

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

You need to pass a function to be called, not call the function when constructing the arguments to then.

Upvotes: 5

Related Questions