flutter - 如何防止再次下载图像
问题描述
您好,我正在尝试下载和共享图像和产品信息,我正在使用 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);
}
}
}
}
解决方案
全局声明列表
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);
}
}
}
}
推荐阅读
- django - 自定义用户管理器:仅填充必填字段
- neural-network - 将网络输出作为另一个网络参数
- dynamics-crm - 无法隐藏/抑制 OOB 模型驱动的应用程序(客户服务中心、销售中心等)
- javascript - Javascript算法最大子数组
- python - 如何根据特定列的值从 CSV 文件中选择值
- java - 检查我们是否可以从源头到目的地
- python - 如何使用 AppleScript 向自己发送 iMessage 并获得通知
- angular - ng serve 不再有效。找不到模块
- python-3.x - Python 3.X 名称错误
- java - BigInteger modPow 与 BigDecimal 指数