selectCover function
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:
- Return first picture with type='Cover' if available
- Fall back to first picture in list
- 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;
}
}