[英]How to listen (StorageUploadTask) Firebase upload Success event in Flutter?
I want to listen to firebase upload success event, so that I can give a confirmation to the user about the completion of the particular event.我想听 firebase 上传成功事件,这样我就可以向用户确认特定事件的完成。
How to listen success event?如何监听成功事件?
In debug console I'm getting the below information.在调试控制台中,我得到以下信息。
I/flutter (22734): EVENT StorageTaskEventType.progress
I/flutter (22734): EVENT StorageTaskEventType.progress
I/flutter (22734): EVENT StorageTaskEventType.progress
I/flutter (22734): EVENT StorageTaskEventType.success
My upload code looks like this.我的上传代码如下所示。
final StorageReference firebaseStoragereference =
FirebaseStorage
.instance
.ref()
.child('images/myImage1.jpg' );
final StorageUploadTask task = firebaseStoragereference.putFile(sampleImage);
You can do it this way:你可以这样做:
final StorageUploadTask uploadTask = ref.putFile(file);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
String downloadUrl = await taskSnapshot.ref.getDownloadURL();
In Order to Listen to event, you Need to listen to the Events Streams & you Can Use StreamBuilder to Show the Status.为了收听事件,您需要收听事件流,并且可以使用 StreamBuilder 来显示状态。
An Minimal Eg: You Modify to As per your Own Need like- show Progress Bar.一个最小的例子:你修改为根据你自己的需要,比如显示进度条。 In your Build Method where you want show the Status call the Function - _uploadStatus(task);
在您想要显示状态的构建方法中调用函数 - _uploadStatus(task);
String _bytesTransferred(StorageTaskSnapshot snapshot) {
double res = snapshot.bytesTransferred / 1024.0;
double res2 = snapshot.totalByteCount / 1024.0;
return '${res.truncate().toString()}/${res2.truncate().toString()}';
}
Widget _uploadStatus(StorageUploadTask task) {
return StreamBuilder(
stream: task.events,
builder: (BuildContext context, snapshot) {
Widget subtitle;
if (snapshot.hasData) {
final StorageTaskEvent event = snapshot.data;
final StorageTaskSnapshot snap = event.snapshot;
subtitle = Text('${_bytesTransferred(snap)} KB sent');
} else {
subtitle = const Text('Starting...');
}
return ListTile(
title: s.isComplete && s.isSuccessful
? Text(
'Done',
style: detailStyle,
)
: Text(
'Uploading',
style: detailStyle,
),
subtitle: subtitle,
);
},
);
}
@anmol.majhail I improved your calc-function like this: @anmol.majhail 我像这样改进了你的计算函数:
String _bytesTransferred(StorageTaskSnapshot snapshot) {
double res = (snapshot.bytesTransferred / 1024.0) / 1000;
double res2 = (snapshot.totalByteCount / 1024.0) / 1000;
return '${res.toStringAsFixed(2)}/${res2.toStringAsFixed(2)}';
}
With this it will look like this:有了这个,它看起来像这样:
3,75/30,32 MB sent已发送 3,75/30,32 MB
Here for Flutter-Web:这里是 Flutter-Web:
String _bytesTransferred(fb.UploadTaskSnapshot snapshot) {
double res = (snapshot.bytesTransferred / 1024.0) / 1000;
double res2 = (snapshot.totalBytes / 1024.0) / 1000;
return '${res.toStringAsFixed(2)}/${res2.toStringAsFixed(2)}';
}
Widget _uploadStatus(fb.UploadTask task, String startingText, String doneText,
String uploadingText) {
return StreamBuilder<fb.UploadTaskSnapshot>(
stream: task.onStateChanged,
builder: (BuildContext context, snapshot) {
Widget subtitle;
if (snapshot.hasData) {
final fb.UploadTaskSnapshot snap = snapshot.data;
subtitle = Text('${_bytesTransferred(snap)} MB');
} else {
subtitle = Text(startingText);
}
return ListTile(
title: snapshot.data.state == fb.TaskState.SUCCESS
? Text(doneText)
: Text(uploadingText),
subtitle: subtitle,
);
},
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.