首页 > 解决方案 > ImagePickerWeb 输出是 File$ 而不是 File

问题描述

我正在使用 ImagePickerWeb 允许用户从我的应用上传照片

  Future<void> getPhotos() async {
    var imageFile = await ImagePickerWeb.getImage(outputType: ImageType.file);

    print(imageFile);

    if (imageFile != null) {
      setState(() {
        currentSelfie = imageFile;

        _accDetails['customer_selfie'] = currentSelfie;
      });
    }

当我尝试通过 Image.File 显示图像时

 Image.file(
                        currentSelfie,
                        height: screenAwareSize(100, context),
                        width: screenAwareSize(100, context),
                        fit: BoxFit.fill,
                      ),

我收到这个错误

File$ ([object File]) :<getObject: NoSuchMethodError: The getter 'uri' was called on null.>

我正在使用我的文件格式,因为我正在将图像传递给我的后端服务器,并将数据作为文件接收。任何帮助,将不胜感激。

标签: flutterflutter-webimagepicker

解决方案


您可以在下面复制粘贴运行完整代码
根据所有者的描述https://github.com/Ahmadre/image_picker_web#how-do-i-get-all-informations-out-of-my-imagevideo-eg-image-and- file-in-one-run
您可以使用ImagePickerWeb.getImageInfo和显示带有Image.memory
代码片段的图像

Future<void> getPhotos() async {
    var mediaData = await ImagePickerWeb.getImageInfo;
    String mimeType = mime(Path.basename(mediaData.fileName));
    html.File mediaFile =
        new html.File(mediaData.data, mediaData.fileName, {'type': mimeType});

    print("imageFile ${mediaData.toString()}");

    if (mediaData != null) {
      currentSelfie = mediaData.data;
      setState(() {});
    }
  }
...  
currentSelfie == null
                ? Container()
                : Image.memory(
                    (currentSelfie),
                    fit: BoxFit.fill,
                  ),  

工作演示

在此处输入图像描述

完整代码

import 'dart:async';
import 'dart:io';
import 'package:mime_type/mime_type.dart';
import 'package:path/path.dart' as Path;
import 'package:flutter/material.dart';
import 'package:image_picker_web/image_picker_web.dart';
import 'dart:html' as html;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var currentSelfie;

  Future<void> getPhotos() async {
    var mediaData = await ImagePickerWeb.getImageInfo;
    String mimeType = mime(Path.basename(mediaData.fileName));
    html.File mediaFile =
        new html.File(mediaData.data, mediaData.fileName, {'type': mimeType});

    print("imageFile ${mediaData.toString()}");

    if (mediaData != null) {
      currentSelfie = mediaData.data;
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            currentSelfie == null
                ? Container()
                : Image.memory(
                    (currentSelfie),
                    fit: BoxFit.fill,
                  ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: getPhotos,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

推荐阅读