用dio上传多个Image Picker(软件包已过期)问题

时间:2020-09-02 11:18:26

标签: flutter dart flutter-dependencies

我以前用下面的代码上传图片,但是由于软件包更新,它不再起作用了,我阅读了文档,但是它不起作用,我也不知道如何使用分段功能

这是代码

import 'package:multi_image_picker/multi_image_picker.dart';
import 'package:dio/dio.dart';
  Future getImage() async {
    files.clear();

    List<Asset> resultList = List<Asset>();
    resultList = await MultiImagePicker.pickImages(
      maxImages: 2,
      enableCamera: false,
    );

    for (var asset in resultList) {
      int MAX_WIDTH = 500; //keep ratio
      int height = ((500 * asset.originalHeight) / asset.originalWidth).round();

      ByteData byteData =
          await asset.requestThumbnail(MAX_WIDTH, height, quality: 80);

      if (byteData != null) {
        List<int> imageData = byteData.buffer.asUint8List();
        UploadFileInfo u = UploadFileInfo.fromBytes(imageData, asset.name);
        files.add(u);
      }
    }

    setState(() {
      _isUploading = true;
    });
  }

  List<UploadFileInfo> files = new List<UploadFileInfo>();
  Future<List<String>> uploadImage() async {
    FormData formData = new FormData.from({"files": files});

    Dio dio = new Dio();
    var response =
        await dio.post("http://localhost:3004/upload", data: formData);

    UploadImage image = UploadImage.fromJson(response.data);
    return image.images;
  }

错误在以下几行

  1. UploadFileInfo u = UploadFileInfo.fromBytes(imageData,asset.name);
  2. List<UploadFileInfo> files = new List<UploadFileInfo>();
  3. FormData formData = new FormData.from({"files": files});

那么我需要做些什么才能使其起作用,以及组成多部分的内容?

任何帮助将不胜感激 预先感谢

1 个答案:

答案 0 :(得分:0)

if (images.isEmpty || images[0] != null) {
                    for (int i = 0; i < images.length; i++) {
                      ByteData byteData = await images[i].getByteData();
                      List<int> imageData = byteData.buffer.asUint8List();
                      http.MultipartFile multipartFile =
                          http.MultipartFile.fromBytes('image', imageData,
                              filename: images[i].name,
                              contentType: MediaType('image', 'jpg'));
                      imagestoEdit.add(multipartFile);
                      print(imagestoEdit.length);
                    }
                  }
Dio.post(url,formdata:{images:imagestoEdit})