如何将multi_image_picker2"资产"转换为"文件"对象?<



我使用multi_image_picker2包来选择多个图像。下面是我的代码。

Future<void> loadAssets() async {
List<Asset> resultList = <Asset>[];
String error = 'No Error Detected';
try {
resultList = await MultiImagePicker.pickImages(
maxImages: 5,
enableCamera: true,
selectedAssets: images,
cupertinoOptions: CupertinoOptions(
takePhotoIcon: "chat",
doneButtonTitle: "Fatto",
),
materialOptions: MaterialOptions(
actionBarColor: "#abcdef",
actionBarTitle: "Pick Your Images",
allViewTitle: "All Photos",
useDetailsView: false,
//selectCircleStrokeColor: "#000000",
),
);
} on Exception catch (e) {
error = e.toString();
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;

setState(() {
images = resultList;
//_error = error;
});
}

我需要将这些转换为File对象,以便我可以将它们上传到Amazon S3。但是如何将Asset对象从multi_image_picker2转换为File对象呢?

您可以使用flutter_absolute_path:

List<File> allImages = [];
resultList.forEach((imageAsset) async {
await FlutterAbsolutePath.getAbsolutePath(imageAsset.identifier)
.then((filePath) {
File tempFile = File(filePath);
if (tempFile.existsSync()) {
allImages.add(tempFile);
}
});
});

相关内容