Home > Back-end >  not getting song files in my app after using flutter audioQuery
not getting song files in my app after using flutter audioQuery

Time:12-07

I've tried every thing to get songfiles on my app but it still shows nothing, here is my code below: '''

   import 'package:flutter/cupertino.dart';

   import 'package:flutter/material.dart';

   import 'package:flutter_audio_query/flutter_audio_query.dart';

    class slist extends StatefulWidget {

@override _slistState createState() => _slistState(); }

    class _slistState extends State<slist> {
    final FlutterAudioQuery audioQuery = FlutterAudioQuery();
     List<SongInfo> songs = [];

      @override
    void initState() {
super.initState();
getAllSongs();

}

      Future<void> getAllSongs() async {
songs = await audioQuery.getSongs();

}

      @override
      Widget build(BuildContext context) {
        return Scaffold(
  backgroundColor: Colors.grey[900],
  body: ListView.builder(
    itemCount: songs.length,
    itemBuilder: (context, index) {
      return ListTile(
        leading: Image.asset(
          songs[index].albumArtwork != null
              ? songs[index].albumArtwork
              : "assets/placeholder.png",
        ),
        title: Text(songs[index].title),
        subtitle: Text(songs[index].artist),
      );
    },
  ),
);

} }

CodePudding user response:

Calling getAllSongs() inside the initState will (if possible) load the songs but not change the widget state, you have two options:

1- Add a setState after getAllSongs():

Future<void> getAllSongs() async {
  songs = await audioQuery.getSongs();
  setState(() {});
}

2- Load the items inside the widget tree:

@override
Widget build(BuildContext context) {
  return Scaffold(
    backgroundColor: Colors.grey[900],
    body: FutureBuilder(
      future: getAllSongs(),
      builder: (context, item) {
        // Null data
        if (item.data == null) return const CircularProgressIndicator();

        // Empty list
        if (item.data!.isEmpty) return const Text("Nothing found!");

        // Load items
        songs = item.data!;
        return ListView.builder(
          itemCount: songs.length,
          itemBuilder: (context, index) {
            return ListTile(
              leading: Image.asset(
                songs[index].albumArtwork != null
                    ? songs[index].albumArtwork
                    : "assets/placeholder.png",
              ),
              title: Text(songs[index].title),
              subtitle: Text(songs[index].artist),
            );
          },
        );
      },
    ),
  );
}

Remember, to get most of the user information you need request the permission

  • Related