简体   繁体   English

将 base64 转换为 Flutter 中的图像错误

[英]Converting base64 to Image in Flutter Error

I have this image as a base64 String which I then want to convert to image in BoxDecoration like so:我将此图像作为 base64 字符串,然后我想将其转换为 BoxDecoration 中的图像,如下所示:

 Container(
                decoration: BoxDecoration(image: DecorationImage(image: Utility.imageFromBase64String(drink.image))),

Here is the function that decodes the String to Image.这是将字符串解码为图像的 function。

Function Function

    class Utility {
  static Image imageFromBase64String(String base64String) {
    return Image.memory(
      base64Decode(base64String),
      fit: BoxFit.fill,
    );
  }}

However, I am getting an error in BoxDecoration:但是,我在 BoxDecoration 中遇到错误:

The argument type 'Image' can't be assigned to the parameter type 'ImageProvider<Object>'

The error is in this part, I suppose I can't put in Image since it asks for ImageProvider:错误在这部分,我想我不能放入 Image,因为它要求 ImageProvider:

image: DecorationImage(image: Utility.imageFromBase64String(drink.image))

Any ideas how to make it work?任何想法如何使它工作? Thanks!谢谢!

The image property from DecorationImage is of type ImageProvider<object> not Image . DecorationImage中的image属性的类型是ImageProvider<object>而不是Image

Return a MemoryImage from imageFromBase64String like soimageFromBase64String返回一个MemoryImage像这样

  static MemoryImage imageFromBase64String(String base64String) {
    return MemoryImage(
      base64Decode(base64String)
    );
  }

Then this.然后这个。

image: DecorationImage(
  image: Utility.imageFromBase64String(drink.image),
  fit: BoxFit.fill,
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM