Ffltter无法解码图像。提供的图像必须是位图。,NULL) [英] Flutter Failed to decode image. The provided image must be a Bitmap., null)

查看:31
本文介绍了Ffltter无法解码图像。提供的图像必须是位图。,NULL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在颤动中的Google地图中创建自定义图标显示错误。

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

                }
            )

        );

这篇关于Ffltter无法解码图像。提供的图像必须是位图。,NULL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆