【发布时间】:2021-09-27 17:50:20
【问题描述】:
我正在创建一个应用程序,它使用 assets_audio_player 从 php 脚本生成的 json 响应播放音乐。有一个返回音频列表的 Future 列表函数。音频不是小部件,所以我不能使用 FutureBuilder。如何使用未来列表?
Future<List<Audio>> creaLista() async {
final response = await http.post(Uri.parse(url));
String responseBody = response.body;
dynamic jsonObject = json.decode(responseBody);
final convertedJsonObject = jsonObject.cast<Map<String, dynamic>>();
List<Song> list =
convertedJsonObject.map<Song>((json) => Song.fromJson(json)).toList();
List<Audio> audioList = list
.map<Audio>((json) => Audio.network(
urlSong + json.url,
metas: Metas(
title: json.title,
artist: json.artist,
album: json.album,
image: MetasImage.network(
urlImage + json.image,
),
),
))
.toList();
return audioList;
}
这是 Song 类:
class Song {
String title;
String artist;
String album;
String image;
String genre;
String url;
Song(
{required this.title,
required this.artist,
required this.album,
required this.image,
required this.genre,
required this.url});
factory Song.fromJson(Map<String, dynamic> json) => Song(
title: json['title'],
artist: json['artist'],
album: json['album'],
image: json['image'],
genre: json['genre'],
url: json['url']);
}
这是 json 响应:
[{"title":"Mille","artist":"Fedez, Achille Lauro, Orietta Berti","album":"Singolo","image":"mille.jpg","genre":"pop","url":"mille.mp3"}]
【问题讨论】:
-
您是否尝试过使用 await 调用它的异步函数,例如 'final audioList = await creaLista();'?
-
要了解如何使用未来列表,您可能想扩展一下使用列表的内容以及使用方式。
-
请分享 HTTP 请求的预期输出以便更好地理解。您还可以明确 Audio 和 Song 是否是两个不同的模型。
-
[{"title":"Mille","artist":"Fedez, Achille Lauro, Orietta Berti","album":"Singolo","image":"mille.jpg", "流派":"pop","url":"mille.mp3"}]
-
我不能使用异步函数,因为我需要在 initState() 中填写 List