NowPlayingTrack.fromJson constructor

NowPlayingTrack.fromJson(
  1. Map<String, dynamic> json
)

Creates a track from json

Returns the static notPlaying instance if player is stopped

Creates image and icon art if not already present/resolved

Implementation

factory NowPlayingTrack.fromJson(Map<String, dynamic> json) {
  if (json.isEmpty) return notPlaying;

  final state = NowPlayingState.values[json['state']];
  if (state == NowPlayingState.stopped) return notPlaying;

  final String imageId = '${json['artist']}:${json['album']}';

  if (!_images.containsKey(imageId)) {
    final Uint8List? imageData = json['image'];
    if (imageData is Uint8List) {
      _images[imageId] = MemoryImage(imageData);
    } else {
      final String? imageUri = json['imageUri'];
      if (imageUri?.startsWith('https://') == true) {
        _images[imageId] = NetworkImage(imageUri!);
      }
    }
  }

  _resolutionStates[imageId] ??= _NowPlayingImageResolutionState.unresolved;

  final Uint8List? iconData = json['sourceIcon'];
  if (iconData is Uint8List) _icons[json['source']] ??= MemoryImage(iconData);

  return NowPlayingTrack(
    id: json['id'].toString(),
    title: json['title'],
    album: json['album'],
    artist: json['artist'],
    duration: Duration(milliseconds: json['duration'] ?? 0),
    position: Duration(milliseconds: json['position'] ?? 0),
    state: state,
    source: json['source'],
  );
}