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

10,402

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: () { 
                  ....

                }
            )

        );
Share:
10,402

Related videos on Youtube

MSARKrish
Author by

MSARKrish

I Interested in working on Android, Now i'm fall in love with Flutter. I have two Apps in Google Play Store. CGPA Calculator New Hand Cricket New

Updated on June 04, 2022

Comments

  • MSARKrish
    MSARKrish almost 2 years

    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)
    
    • Dev
      Dev about 4 years
      have you configured pubspec for assets. I think you are missing configuration
    • MSARKrish
      MSARKrish about 4 years
      @Dev i added that image in assets folder and mentioned in pubspec file also brother.
    • Dev
      Dev about 4 years
      what is the code for create marker where it is used
    • MSARKrish
      MSARKrish about 4 years
      @Dev i added my code for creating marker in my question and i called that method in initstate of my class
  • giorgio79
    giorgio79 almost 4 years
    Probably it is better to convert the image to bmp offline. Image clients converting an image all the time...
  • Sunit Gautam
    Sunit Gautam over 3 years
    @giorgio79 The problem I faced while converting png to bmp online was that they compulsorily filled the transparent background area with a solid colour. The above solution worked fine.
  • Ojonugwa Jude Ochalifu
    Ojonugwa Jude Ochalifu about 3 years
    Where does ui come from? Please always try to provide full solutions. Besides, this code was copied from here javaer101.com/en/article/38474402.html
  • Ritesh Singh
    Ritesh Singh over 2 years
    Add import 'dart:ui' as ui;
  • Emre Akcan
    Emre Akcan about 2 years
    this doesn't work anymore
  • Dev
    Dev about 2 years
    Its been a long flutter and plugins have changed a lot in long run. If you still need a working solution then post a new question as this was based on flutter versions available that time.