selectCover function

Picture? selectCover(
  1. List<Picture>? pictures
)

Select the best cover art from a list of pictures.

Selects the most appropriate cover image from a list of pictures. Prioritizes pictures with type='Cover', falls back to the first picture, or returns null if the list is empty.

Parameters:

  • pictures: List of Picture objects, or null

Returns: Best Picture object, or null if list is empty/null

Selection logic:

  1. Return first picture with type='Cover' if available
  2. Fall back to first picture in list
  3. Return null if list is empty or null

Example:

final pictures = [
  Picture(format: 'image/jpeg', data: [...], type: 'Back'),
  Picture(format: 'image/jpeg', data: [...], type: 'Cover'),
  Picture(format: 'image/png', data: [...], type: null),
];
final cover = selectCover(pictures);
// Returns the picture with type='Cover'

Implementation

Picture? selectCover(List<Picture>? pictures) {
  if (pictures == null || pictures.isEmpty) {
    return null;
  }

  // Try to find a picture with type='Cover'
  try {
    return pictures.firstWhere((p) => p.type == 'Cover');
  } catch (_) {
    // No picture with type='Cover' found, return first picture
    return pictures.isNotEmpty ? pictures.first : null;
  }
}