Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

not getting song files in my app after using flutter audioQuery

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();

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      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),
      );
    },
  ),
);

}
}

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading