【问题标题】:How to get List from future list function in Dart如何从 Dart 中的未来列表函数中获取列表
【发布时间】: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

标签: flutter dart https


【解决方案1】:

您不需要Future 来返回Widget 来使用FutureBuilder。您可以根据未来的返回结果创建小部件。这是一个仅显示艺术家的示例,但它应该给你这个想法。

一般来说,我建议创建一个自定义 Widget,它接受一个 Audio 对象,并以您想要的方式显示所有 Audio 数据。

FutureBuilder(
          future: creaLista(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              final audioList = snapshot.data as List<Audio>;
              return ListView.builder(
                  itemCount: audioList.length,
                  itemBuilder: (context, index) {
                    return Text(audioList[index].artist);
                  });
            } else if (snapshot.hasError) {
              // handle error here
              return Text('${snapshot.error}');
            } else {
              return CircularProgressIndicator(); // displays while loading data
            }
          },
        )

这假设您的 Audio 类看起来像这样

class Audio {
  String title, artist, album;
  Audio(this.title, this.artist, this.album);
}

我相信它比这更复杂,但它应该给你的想法。

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2020-05-18
    • 1970-01-01
    相关资源
    最近更新 更多