这是显示我如何从 Firebase 实时数据库获取数据的代码:
List musicList = <Song>[];
_MusicAppState() {
FirebaseDatabase.instance.reference().child("${getCurrentUID()}").once().then((dataSnapshot){
print("Sucessfully loaded the data");
List tmpList = [];
dataSnapshot.value.forEach((k,v){
tmpList.add(v);
});
musicList = tmpList;
setState(() {
});
}).catchError((error) {
print("Failed to loaded the data");
});
}
这是我在 Listview 中显示音乐列表的部分:
Expanded(
child: ListView.builder(
// List musicList = <Song>[];
itemCount: musicList.length,
itemBuilder: (context, index) => customListTitle(
onTap: () {
playMusic(musicList[index]['url']);
setState(() {
_currentTitle = musicList[index]['title'];
_currentSinger = musicList[index]['singer'];
_currentCover = musicList[index]['cover'];
});
},
title: musicList[index]['title'],
singer: musicList[index]['singer'],
cover: musicList[index]['cover'],
),
),
我正在尝试构建一些搜索功能,但都没有奏效。 这是我执行的搜索功能之一:
Widget buildSearch() => SearchWidget(
text: query,
hintText: 'Songs name',
onChanged: searchSong,
);
void searchSong(String query) {
final songs = musicList.where((song) {
final titleLower = song.title.toLowerCase();
final searchLower = query.toLowerCase();
return titleLower.contains(searchLower);
}).toList();
setState(() {
this.query = query;
this.musicList = songs;
});
}
}
这也是我的 Song.dart 文件:
class Song {
final String title;
final String singer;
final String cover;
final String url;
const Song({
required this.title,
required this.singer,
required this.cover,
required this.url,
});
}
有人可以让我知道我的代码有什么问题吗?我的搜索功能不起作用