'Why is this List returning null?

I have this code here in my provider, whenever I call getSongs the returned mp3s is null. I have it coded this way so that I can memoize mp3s.

class Songs with ChangeNotifier {
  final tagger = Audiotagger();

  List<Tag?>? mp3s;

  Future<Tag?> getTags(String path) async {
    final Tag? tag = await tagger.readTags(path: path);
    return tag;
  }

  Future<Uint8List?> getArtwork(String path) async {
    final Uint8List? artwork = await tagger.readArtwork(path: path);
    return artwork;
  }

  Future<List<Tag?>> getSongs() async {
    if (mp3s == null) {
      final directory = Directory('/storage/emulated/0/Download');
      List<FileSystemEntity> _files;
      List<Tag?> _songs = [];

      _files = directory.listSync(recursive: true, followLinks: false);

      for (FileSystemEntity entity in _files) {
        String path = entity.path;
        if (path.endsWith('.mp3')) {
          _songs.add(await getTags(path));
        }
      }

      mp3s = _songs;
    } else {
      return mp3s!;
    }
    return mp3s!;
  }
}



Solution 1:[1]

Actually, the problem to this is that there's a mp3 file that threw an error, because of this, it caused _songs to be null and therefore mp3s to be null.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1