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: () {
....
}
)
);
Related videos on Youtube
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, 2022Comments
-
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 about 4 yearshave you configured pubspec for assets. I think you are missing configuration
-
MSARKrish about 4 years@Dev i added that image in assets folder and mentioned in pubspec file also brother.
-
Dev about 4 yearswhat is the code for create marker where it is used
-
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 almost 4 yearsProbably it is better to convert the image to bmp offline. Image clients converting an image all the time...
-
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 about 3 yearsWhere 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 over 2 yearsAdd
import 'dart:ui' as ui;
-
Emre Akcan about 2 yearsthis doesn't work anymore
-
Dev about 2 yearsIts 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.