how to sort list of songs from external storage alphabetically in flutter

筅森魡賤 提交于 2021-01-28 15:01:05

问题


As my Problem Says..i want to sort my listView items that are basically songs read by external storage..I want to sort them alphabetically..this is my code for listView

final List<MaterialColor> _colors = Colors.primaries;
@override
Widget build(BuildContext context) {
final rootIW = MPInheritedWidget.of(context);
SongData songData = rootIW.songData;
return new ListView.builder(
  itemCount: songData.songs.length,
  itemBuilder: (context, int index) {
    var s = songData.songs[index];
    final MaterialColor color = _colors[index % _colors.length];
    var artFile =
        s.albumArt == null ? null : new File.fromUri(Uri.parse(s.albumArt));

    return new ListTile(
      dense: false,
      leading: new Hero(
        child: avatar(artFile, s.title, color),
        tag: s.title,
      ),
      title: new Text(s.title),
      subtitle: new Text(
        "By ${s.artist}",
        style: Theme.of(context).textTheme.caption,
      ),

回答1:


Lists can be sorted very easily in Dart.

To sort alphabetically by song title add this line to the beginning of your build method (or wherever else you want to sort the songs list):

Widget build(BuildContext context) {
  songData.songs.sort((a, b) => a.title.compareTo(b.title));

Note that this sorts the list in place, meaning the sort method does not return a sorted copy of the list, but sorts the existing list directly.

Also check out this other answer.



来源:https://stackoverflow.com/questions/54065146/how-to-sort-list-of-songs-from-external-storage-alphabetically-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!