MSARKrish
MSARKrish

Reputation: 4144

Flutter Failed to decode image. The provided image must be a Bitmap., null)

Creating custom Icon in Google Map in flutter shows error.

My pubspec.yaml file

assets:
  - assets/truck.png

My Code is:

void getCustomIcon() async {
    customIcon = await BitmapDescriptor.fromAssetImage(
        ImageConfiguration(
          devicePixelRatio: 2.5,
    ),
    'assets/truck.png');
}

Error is:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, Failed to decode image. The provided image must be a Bitmap., null)
E/flutter (15757): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (15757): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33)

Upvotes: 5

Views: 22216

Answers (1)

Dev
Dev

Reputation: 6786

define in stateclass

BitmapDescriptor customIcon ;

call in initState

getBytesFromAsset('assets/truck.png', 64).then((onValue) {
      customIcon =BitmapDescriptor.fromBytes(onValue);

    });

where function is

  static Future<Uint8List> getBytesFromAsset(String path, int width) async {
    ByteData data = await rootBundle.load(path);
    ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List(), targetWidth: width);
    ui.FrameInfo fi = await codec.getNextFrame();
    return (await fi.image.toByteData(format: ui.ImageByteFormat.png)).buffer.asUint8List();
  }

then in marker creation

markers.add(
            Marker(
                markerId: ....,
                position: ....,
                icon: customIcon ,
                 onTap: () { 
                  ....

                }
            )

        );

Upvotes: 12

Related Questions