import 'package:flutter/cupertino.dart'; import 'package:wechat_assets_picker/wechat_assets_picker.dart'; import 'package:photo_manager/photo_manager.dart'; class FileSizeCalculatorUtil { static final Map fileSizeCache = {}; /// 获取文件大小 static Future getFileSize(String assetId) async { if (fileSizeCache.containsKey(assetId)) { return fileSizeCache[assetId]!; } final entity = await AssetEntity.fromId(assetId); if (entity == null) return 0; final file = await entity.file; if (file == null) return 0; double size = (await file.length()) / 1024; if ( await file.exists()) { try { await file.delete(); } catch (e) { debugPrint("Delete file error: $e"); } } else { debugPrint("File not exists: ${file.path}"); } if (size <= 0) { return 0; } fileSizeCache[assetId] = size; return size; } }