无论用户是否导航到另一个屏幕,我都希望从一个屏幕启动后在后台继续异步任务。
在“图像列表”屏幕中,用户可以选择多张图像,然后将开始图像上传。上传图像的功能如下。
dynamic _onUploadAllAssets(String albumId, AlbumManager albumManager,
List<Asset> imageList, context, albumBloc) async {
for (int i = 0; i < imageList.length; i++) {
await uploadAsset(
imageList.first, albumId, albumManager, 0, context, albumBloc);
albumBloc.fetchAllImages(albumId);
imageList.remove(imageList.first);
albumBloc.getSelectedImage(imageList);
}
}
dynamic uploadAsset(Asset asset, String albumId, AlbumManager albumManager,
int index, context, albumBloc) async {
final String uuid = Uuid().v1();
final height = MediaQuery.of(context).size.height / 2;
final width = asset.originalWidth * height / asset.originalHeight;
final imageByte = await asset.getByteData(quality: 100);
final base64Image = base64Encode(imageByte.buffer.asUint8List());
await uploadAlbumImage(albumId, base64Image, uuid, false, index, albumBloc);
final compressedImageBytes = await asset.getThumbByteData(
width.round(),
height.round(),
);
final compressedImageU8 = compressedImageBytes.buffer.asUint8List();
final compressedBase64 = base64Encode(compressedImageU8);
await uploadAlbumImage(
albumId, compressedBase64, uuid, true, index, albumBloc);
}
Future<bool> uploadAlbumImage(String albumId, String base64Image, String uuid,
bool isThumbnail, int index, albumBloc) async {
await albumManager.connectFirebase();
final Directory systemTempDir = Directory.systemTemp;
if (albumManager.circleAlbumCacheDir == null) {
await albumManager.getCacheDirectory(albumId);
}
final encryptor = encryption.StringEncryption();
final encryptedImage = await encryptor.getEncryption(base64Image);
final String prefix = isThumbnail ? 'thumbnail_' : 'img_';
final File file = File('${systemTempDir.path}/tmp$prefix$uuid.txt');
if (file.existsSync()) {
await file.delete();
}
await file.create();
await file.writeAsString(encryptedImage);
final StorageReference preRef =
albumManager.storageReference.child(albumId);
final StorageReference thumbnailRef =
preRef.child('thumbnail').child('thumbnail_$uuid.txt');
final StorageReference imageRef =
preRef.child('images').child('img_$uuid.txt');
final StorageReference ref = isThumbnail ? thumbnailRef : imageRef;
final StorageUploadTask task = ref.putFile(
file,
StorageMetadata(
contentLanguage: 'en',
customMetadata: <String, String>{'activity': 'uploadEventImage'},
),
);
task.events.listen((storageTaskEvent) {
final _progess = storageTaskEvent.snapshot.bytesTransferred.toDouble() /
storageTaskEvent.snapshot.totalByteCount.toDouble();
albumBloc.syncFileUploading(uuid, isThumbnail, _progess, index);
});
await task.onComplete;
if (!task.isSuccessful) {
ref.delete();
await file.delete();
} else if (isThumbnail) {
final String thumbnailUrl = await thumbnailRef.getDownloadURL();
final String imageUrl = await imageRef.getDownloadURL();
await albumManager.addImageToFolder(
albumId, uuid, thumbnailUrl, imageUrl, true);
await albumManager.getCacheDirectory(albumId);
albumManager.cacheImage(
base64Image, '${albumManager.circleAlbumCacheDir}/thumb_$uuid.png');
} else {
await albumManager.getCacheDirectory(albumId);
albumManager.cacheImage(
base64Image, '${albumManager.circleAlbumCacheDir}/img_$uuid.png');
}
await file.delete();
return task.isSuccessful;
}
问题是,一旦图像上传开始,并且用户导航到另一个屏幕,则正在上传的图像将完成,但不会上传剩余的图像。
对于For循环,当用户导航到另一个屏幕或返回上一个屏幕时,停止执行。我想继续执行for循环,直到所有图像上传完毕。
答案 0 :(得分:0)
将_onUploadAllAssets嵌入另一个异步函数中应该做的工作没有吗? (不使用await关键字...)
答案 1 :(得分:0)
如果您想在后台运行而不向用户显示进度的任何线索,则必须使用“隔离”。但是,如果要向用户显示任务的进度,请创建一个包含progressBar作为窗口小部件的窗口小部件,并在每个页面中将其用作子窗口小部件。