alexmercerind / flutter_media_metadata

A Flutter plugin to read 🔖 metadata of 🎵 media files. Supports Windows, Linux, macOS, Android, iOS & Web.
MIT License
70 stars 40 forks source link

Error on Flutter web for media files without album art #28

Open 50n13x opened 2 years ago

50n13x commented 2 years ago

Hi,

Following piece of code:

FilePickerResult? result = await FilePicker.platform.pickFiles(allowMultiple: true);
  if (result != null) {
  for(int x = 0; x < result.files.length; x++) {
    print(result.files[x].name);
    Uint8List bytes = result.files[x].bytes as Uint8List;
    final metadata = await MetadataRetriever.fromBytes(bytes); // throws the error
    String? trackName = metadata.trackName;
    print(trackName);

[...]

I checked this with mp3, m4a and wmv files. It only occurs on Flutter web. If the audio files contain an album art everything works fine. If they do not contain an album art, I get the following error message:

Error: Expected a value of type 'String', but got one of type 'Null' at Object.throw_ [as throw] (http://localhost:50323/dart_sdk.js:5067:11) at Object.castError (http://localhost:50323/dart_sdk.js:5026:15) at Object.cast [as as] (http://localhost:50323/dart_sdk.js:5351:17) at Function.as (http://localhost:50323/dart_sdk.js:46170:19) at http://localhost:50323/packages/flutter_media_metadata/src/flutter_media_metadata_web.dart.lib.js:89:159 at Object._checkAndCall (http://localhost:50323/dart_sdk.js:5274:16) at Object.dcall (http://localhost:50323/dart_sdk.js:5279:17) at ret (http://localhost:50323/dart_sdk.js:62346:21)

Afaik this relates to the following line of code:

let metadata = new (T.IdentityMapOfString$dynamic()).from(["metadata", new _js_helper.LinkedMap.new(), "albumArt", convert.base64Decode(core.String.as(dart.dsend(rawMetadataJson, '_get', ["Cover_Data"]))), "filePath", null]);

I cannot read nor write .js :) but it looks like a null check is missing?

What else do you need?

Thanks, Michael

50n13x commented 2 years ago

FYI, with the following quick fix it seems to work:

flutter_media_metadata_web.dart: line 109 -113:

           final metadata = <String, dynamic>{
              'metadata': {},
              'albumArt': base64Decode(rawMetadataJson['Cover_Data']),
              'filePath': null,
            };

replaced by:

            var metadata = <String, dynamic>{};
            if (rawMetadataJson['Cover_Data'] != null) {
              metadata = <String, dynamic>{
                'metadata': {},
                'albumArt': base64Decode(rawMetadataJson['Cover_Data']),
                'filePath': null,
              };
            } else {
              metadata = <String, dynamic>{
                'metadata': {},
                'albumArt': null,
                'filePath': null,
              };
            }

Haven't yet checked if an album art is derived it the file contains one.

iliyami commented 1 year ago

It's right and I have the same issue.

Error: Expected a value of type 'String', but got one of type 'Null' dart-sdk/lib/_internal/js_dev_runtime/private/ddcruntime/errors.dart 266:49 throw dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 99:3 castError dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 452:10 cast dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 705:17 as packages/flutter_media_metadata/src/flutter_media_metadata_web.dart 111:31 dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14 _checkAndCall dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39 dcall Application finished.