Click here to Skip to main content
15,067,251 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
so I am making an Music player app in flutter and want to show a list of all songs fetched from the internal storage of device but can't see to find the correct code. Thank you.


import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:path_provider/path_provider.dart';

class Songlist extends StatefulWidget {
@override
_SonglistState createState() => _SonglistState();
}

class _SonglistState extends State<Songlist> {
@override
void initState() {
super.initState();
initlist();
}

void initlist() async {
if (await Permission.storage.request().isGranted) {
  Directory dir = await getExternalStorageDirectory();
  List<FileSystemEntity> files;
  files = dir.listSync(recursive: true, followLinks: false);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text("Your Media"),
    backgroundColor: mycolor,
  ),
  /* body: new ListView.builder(
      padding: const EdgeInsets.all(16.0),
      itemCount: files.length,
      itemBuilder: (context, i) {
        return buildRow(_files.elementAt(i).path);
      }), */
  );
}
}


What I have tried:

I tried to use flute_player_plugin but it doesn't work it has some issues
Posted
Updated 9-Oct-20 5:42am

1 solution

You're adding your files to a "local variable" and then letting it "disappear" (go out of scope).

You need to add "files" (names) to your ListView.

List<FileSystemEntity> files;
files = dir.listSync(recursive: true, followLinks: false);
   
Comments
SagarRawat 10-Oct-20 10:04am
   
how do I use the file name in listview can you tell me , if that's okay
David Crow 11-Oct-20 11:02am
   
Add the items to an adapter. Associate the adapter with the ListView.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900