首页 > 解决方案 > 如何防止再次下载图像

问题描述

您好,我正在尝试下载和共享图像和产品信息,我正在使用 imagepicker saver 和 esys 包,我想知道我是否可以阻止两次图像的下载。我该如何实现。

基于平台的自定义按钮

AppButton(
                  padding: EdgeInsets.all(0),
                  isIOS: isIOS,
                  height: 60,
                  width: MediaQuery.of(context).size.width * 0.45,
                  icon: FrinoIcons.f_share,
                  buttonType: ButtonType.Resell,
                  text: 'Share Product',
                  onPressed: () => onImageDownLoad(product),
                )

//功能

void onImageDownLoad(Product product) async {
      List list = [];
      final String text = "${product.name}\n\n${product.description}";
      var req = await HttpClient().getUrl(Uri.parse(product.image[0]));
      var response = await req.close();
      Uint8List bytes = await consolidateHttpClientResponseBytes(response);
      if (list.length == 0) {
        list.add(File.fromRawPath(bytes));
        await ImagePickerSaver.saveFile(fileData: bytes).whenComplete(() {
          EsysShare.Share.file('ESYS AMLOG', 'amlog.jpg', bytes, 'image/jpg',
              text: text);
        });
      } else {
        for (Uint8List items in list) {
          print(list);
          if (items == bytes) {
            EsysShare.Share.file('ESYS AMLOG', 'amlog.jpg', items, 'image/jpg',
                text: text);
          }
        }
      }
    }

标签: flutter

解决方案


全局声明列表

List list = [];

void onImageDownLoad(Product product) async {
           final String text = "${product.name}\n\n${product.description}";
      var req = await HttpClient().getUrl(Uri.parse(product.image[0]));
      var response = await req.close();
      Uint8List bytes = await consolidateHttpClientResponseBytes(response);
      if (list.length == 0) {
        list.add(File.fromRawPath(bytes));
        await ImagePickerSaver.saveFile(fileData: bytes).whenComplete(() {
          EsysShare.Share.file('ESYS AMLOG', 'amlog.jpg', bytes, 'image/jpg',
              text: text);
        });
      } else {
        for (Uint8List items in list) {
          print(list);
          if (items == bytes) {
            EsysShare.Share.file('ESYS AMLOG', 'amlog.jpg', items, 'image/jpg',
                text: text);
          }
        }
      }
    }

推荐阅读