我正在尝试从我的应用程序目录中提取数据(mp3),并在ListView()
中显示,即路径:/storage/emulated/0/Android/data/com.example.MyApp/files
为此,我使用了getExternalStorageDirectory()将我的文件保存在那里,但是使用以下代码只能获得通知声音。 我只需要来自上述路径的数据。 指导我。
import 'dart:io';
import 'package:cast/utilities/bottom_sheet_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
class MyAudioList extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyAudioList(); //create state
}
}
class _MyAudioList extends State<MyAudioList> {
var files;
List? allSongs;
void getFiles() async {
//asyn function to get list of files
Directory dir =
Directory('/storage/emulated/0/Android/data/com.example.cast/files');
String mp3Path = dir.toString();
print(mp3Path);
List<FileSystemEntity> _files;
List<FileSystemEntity> _songs = [];
_files = dir.listSync(recursive: true, followLinks: false);
for (FileSystemEntity entity in _files) {
String path = entity.path;
if (path.endsWith('.mp3')) _songs.add(entity);
}
print(_songs);
// print(_songs.length);
setState(() {
files = _files;
allSongs = _songs;
}); //update the UI
}
@override
void initState() {
getFiles(); //call getFiles() function on initial state.
super.initState();
}
@override
Widget build(BuildContext context) {
return RefreshIndicator(
onRefresh: () async {
getFiles();
},
child: Scaffold(
body: files == null
? Center(
child: Text(
'Seaching..',
style: TextStyle(color: Colors.white),
))
: ListView.builder(
//if file/folder list is grabbed, then show here
itemCount: allSongs!.length, //_songs.length ?? 0,
itemBuilder: (context, index) {
return ListTile(
key: UniqueKey(),
leading: Row(
mainAxisSize: MainAxisSize.min,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.network(
"https://images.unsplash.com/photo-
1541963463532-d68292c34b19?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8Ym9va3xlbnwwfHwwfHw%3D&w=1000&q=80",
width: 50,
height: 50,
fit: BoxFit.cover,
),
),
],
),
title: Text(
'$allSongs',
maxLines: 1,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.w400,
),
),
subtitle: Text(
"publisher",
style: TextStyle(
color: Colors.grey[400]!,
fontSize: 13,
),
),
trailing: Row(mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: () {},
icon: Icon(Icons.more_vert, color:
Colors.grey),
)
]),
);
},
)),
);
}
}