Flutter 启动后继续异步任务,即使用户在flatter中导航到另一个屏幕
我想在后台继续异步任务,一旦从一个屏幕开始,不管用户是否导航到另一个屏幕 从图像列表屏幕用户可以选择多个图像和图像上传将开始。上传图片的功能如下Flutter 启动后继续异步任务,即使用户在flatter中导航到另一个屏幕,flutter,dart,async-await,Flutter,Dart,Async Await,我想在后台继续异步任务,一旦从一个屏幕开始,不管用户是否导航到另一个屏幕 从图像列表屏幕用户可以选择多个图像和图像上传将开始。上传图片的功能如下 dynamic _onUploadAllAssets(String albumId, AlbumManager albumManager, List<Asset> imageList, context, albumBloc) async { for (int i = 0; i < imageList.length;
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;
}
dynamic\u onUploadAllAssets(字符串albumId、AlbumManager、AlbumManager、,
列表imageList、context、albumBloc)异步{
对于(int i=0;i
问题是一旦图像上传开始,用户导航到另一个屏幕,则正在上传的图像将完成,但不会上载剩余的图像
当用户导航到另一个屏幕或返回到上一个屏幕时,For循环停止执行。我想继续执行for循环,直到上传所有图像。在另一个异步函数中嵌入onUploadAllAssets应该可以完成任务否?(不使用wait关键字…在另一个异步函数中嵌入onUploadAllAssets应该执行此任务否?(不使用wait关键字…如果您想在后台运行,而不向用户显示任何进度线索,则必须使用“隔离”。但是,如果您想向用户显示任务的进度,则创建一个包含进度条的小部件作为小部件,并在每个页面中将其作为子小部件使用。如果您想在后台运行,而不向用户显示任何进度线索,则必须使用“隔离”。但是,如果您想向用户显示任务的进度,那么创建一个包含progressBar的小部件作为小部件,并在每个页面中将其作为子小部件使用