使用 flutter audioQuery 后我的应用程序中没有获取歌曲文件
not getting song files in my app after using flutter audioQuery
我已尽一切努力在我的应用程序上获取歌曲文件,但它仍然没有显示任何内容,下面是我的代码:
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),
);
},
),
);
}
}
在 initState
中调用 getAllSongs()
将(如果可能)加载歌曲但不会更改小部件状态,您有两个选择:
1-在getAllSongs()
后添加一个setState
:
Future<void> getAllSongs() async {
songs = await audioQuery.getSongs();
setState(() {});
}
2- 加载小部件树中的项目:
@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),
);
},
);
},
),
);
}
记住,要获取您需要的大部分用户信息request the permission
我已尽一切努力在我的应用程序上获取歌曲文件,但它仍然没有显示任何内容,下面是我的代码:
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),
);
},
),
);
}
}
在 initState
中调用 getAllSongs()
将(如果可能)加载歌曲但不会更改小部件状态,您有两个选择:
1-在getAllSongs()
后添加一个setState
:
Future<void> getAllSongs() async {
songs = await audioQuery.getSongs();
setState(() {});
}
2- 加载小部件树中的项目:
@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),
);
},
);
},
),
);
}
记住,要获取您需要的大部分用户信息request the permission