Now I am facing a problem, this is my Dart code:
Future<List<Music>?> getAvaliableFmMusics(List<Music>? recommand) async {
final List<Music> resultMusic = List.empty(growable: true);
if (recommand != null) {
recommand.forEach((element) async {
bool isLegacyMusic = await ReddwarfMusic.legacyMusic(element);
if (!isLegacyMusic) {
resultMusic.add(element);
}
});
}
return resultMusic;
}
I get a recommand music list, the next step I check the music list is contains legacy music(user already listening for many times), then add the filtered music to a new list. Finnaly return the filtered music list to the user UI. The problem is that the legacy music will send an async http requst, the resultMusic
always return null. What I want to do is waiting the foreach complete then return the filtered resultMusic
,what should I do to make it work like that? is it possible to do like this?
CodePudding user response:
To work with forEach
and Futures
you should use the Future.forEach
Like so:
await Future.forEach(yourList,(e) async {
await asyncCall(e);
});