Skip to content

How can i make a dinamic List<Audio> audioList? #4

@Ednokia777

Description

@Ednokia777

How can i make List audioList [
....
]
with dynamic data from json? I've already realized it in 'for' but it doesn't work correct

final AssetsAudioPlayer audioPlayer = AssetsAudioPlayer();
  final SwiperController swiperController = SwiperController();
  double screenWidth = 0;
  double screenHeight = 0;
  var currentSongs;
  Color highlightColor = Colors.white;
  var audio;
  var title;
  var subtitle;
  var image;
  late List allSongs = [];
  List<Audio> audioList = [];


  @override
  void dispose() {
    audioPlayer.stop();
    super.dispose();
  }

  @override
  void initState() {
    setupPlaylist();
    super.initState();
  }

  void setupPlaylist() async {
    currentSongs = widget.data['index'];
    allSongs = widget.data['songs'];
    for (var i = 0; i < allSongs.length; i++) {
      audio = allSongs[i]['fields']['audio'][0]['url'].toString();
      title = allSongs[i]['fields']['name'].toString();
      subtitle = allSongs[i]['fields']['type'].toString();
      image = allSongs[i]['fields']['cover'][0]['url'].toString();
      audioList.add(
        Audio.network(
            audio, metas: Metas(
            title: title,
            artist: subtitle,
            image: MetasImage.file(image)
        )
        ),
      );
    }
    audioPlayer.open(
      Playlist(audios: audioList, startIndex: currentSongs),
      autoStart: false,
      showNotification: true,
      loopMode: LoopMode.none

    );
  }

I used it https://github.com/FlorianPruemer/flutter_tutorials/blob/master/audio_players/lib/design_examples/episode6_playlist_view.dart

Help me, please.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions