Fireworks
Fireworks

Reputation: 3

How to add album arts in flutter

I am using Flute music player plugin to make music player app in Flutter. But I am having a trouble for adding album art.

I wrote:

  dynamic getImage(int idx) {
      return _songs[idx].albumArt == null
      ? null
      : new File.fromUri(Uri.parse(_songs[idx].albumArt));
  }

And I used Image.file Widget:

Container(
    childe: Image.file(getImage(_index))
)

And the result is:

I/flutter (15576): The following assertion was thrown building HyperMusicHome(dirty, state:
I/flutter (15576): _HyperMusicHomeState#b83f2):
I/flutter (15576): 'package:flutter/src/painting/image_provider.dart': Failed assertion: line 621 pos 14: 'file !=
I/flutter (15576): null': is not true.
I/flutter (15576):
I/flutter (15576): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (15576): more information in this error message to help you determine and fix the underlying cause.
I/flutter (15576): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (15576):   https://github.com/flutter/flutter/issues/new?template=BUG.md

Upvotes: 0

Views: 1156

Answers (1)

Niklas Higi
Niklas Higi

Reputation: 2309

The error you're getting comes from this line in Flutter's FileImage class which checks whether what you're passing to Image.file() is null. It seems like the song you're currently viewing does not have an album art. All you need to do is not show the image when no album art is available.

I don't know what exactly your widget looks like, but you could do something like this:

@override
Widget build(BuildContext context) {
  // Call `getImage` once to get the image file (which might be `null`)
  final albumArtFile = getImage(_index);

  return Container(
    // Only show the image if `albumArtFile` is not `null`
    child: albumArtFile != null ? Image.file(albumArtFile) : null,
  );
}

You could also bundle a placeholder image with your app and show that when no album art is available:

albumArtFile != null ? Image.file(albumArtFile) : Image.asset('assets/placeholder.png')

You can learn more about adding assets to your app here.

Upvotes: 1

Related Questions