gpt4 book ai didi

google-maps - Flutter无法解码图像。提供的图像必须是位图。

转载 作者:行者123 更新时间:2023-12-03 03:11:52 24 4
gpt4 key购买 nike

在Google Map中快速创建自定义图标会显示错误。

我的pubspec.yaml文件

assets:
- assets/truck.png

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

错误是:
[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)

最佳答案

在状态类中定义

BitmapDescriptor customIcon ;

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

});

功能在哪里
  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();
}

然后在标记创建中
markers.add(
Marker(
markerId: ....,
position: ....,
icon: customIcon ,
onTap: () {
....

}
)

);

关于google-maps - Flutter无法解码图像。提供的图像必须是位图。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60111721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com