如何在Flutter网站中显示dart:html文件图像?

时间:2020-08-27 09:10:40

标签: html image flutter dart flutter-web

我通过这种方式从用户设备上传文件:

void uploadImage() async {
    html.InputElement uploadInput = html.FileUploadInputElement();
    uploadInput.click();
    uploadInput.onChange.listen(
      (changeEvent) {
        final file = uploadInput.files.first;
        final reader = html.FileReader();

        reader.readAsDataUrl(file);
        reader.onLoadEnd.listen(
          (loadEndEvent) async {
            setState(() {
              image = file; //image is a dart:html File object, it's a field of my statefulwidget
            });
          },
        );
      },
    );
  }
}

我想显示这张图片。我尝试使用Image.file:

           Container(
              width: 100,
              height: 100,
              child: Image.file(
                image,
                fit: BoxFit.contain,
              ),
            ),

但是这给了我这个错误:

无法将参数类型“文件”(其中文件在C:\ Users \ Asus \ Documents \ flutter \ bin \ cache \ pkg \ sky_engine \ lib \ html \ html_dart2js.dart中定义)分配给参数类型“ File” (其中File在C:\ Users \ Asus \ Documents \ flutter \ bin \ cache \ pkg \ sky_engine \ lib \ io \ file.dart中定义)'。dartargument_type_not_assignable html_dart2js.dart(15975,7):文件在C:\ Users \ Asus \ Documents \ flutter \ bin \ cache \ pkg \ sky_engine \ lib \ html \ html_dart2js.dart中定义 file.dart(241,16):文件在C:\ Users \ Asus \ Documents \ flutter \ bin \ cache \ pkg \ sky_engine \ lib \ io \ file.dart中定义 偷看问题(Alt + F8) 没有可用的快速修复

有没有办法将dart:html文件转换为dart:io文件?还是我应该尝试另一种显示图像的方式?在这种情况下,如何显示dart:html文件?

谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

一种解决方案是使用image_whisperer软件包。

让我们说您的html.File是变量myfile,以下对我有用:

import 'package:flutter/painting.dart';  // NetworkImage
import 'package:image_whisperer/image_whisperer.dart';  // BlobImage
...

BlobImage blobImage = new BlobImage(myfile, name: myfile.name);
final image = NetworkImage(blobImage.url);

// Use image here
...