如何在Flutter中收听(StorageUploadTask)Firebase上传成功事件?

时间:2018-12-05 17:24:05

标签: dart flutter

我想收听Firebase上传成功事件,以便可以向用户确认特定事件的完成。

如何收听成功事件?

在调试控制台中,我得到以下信息。

I/flutter (22734): EVENT StorageTaskEventType.progress
I/flutter (22734): EVENT StorageTaskEventType.progress
I/flutter (22734): EVENT StorageTaskEventType.progress
I/flutter (22734): EVENT StorageTaskEventType.success

我的上传代码如下。

final StorageReference firebaseStoragereference = 
                          FirebaseStorage
                          .instance
                          .ref()
                          .child('images/myImage1.jpg' );

final StorageUploadTask task = firebaseStoragereference.putFile(sampleImage);

4 个答案:

答案 0 :(得分:1)

此处为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,
            );
          },
        );
      }

答案 1 :(得分:0)

您可以这样做:

final StorageUploadTask uploadTask = ref.putFile(file);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
String downloadUrl = await taskSnapshot.ref.getDownloadURL();

答案 2 :(得分:0)

为了收听事件,您需要收听事件流,并且可以使用StreamBuilder显示状态。

最低限度:例如,您将自己修改为根据自己的需求显示进度条。在要显示状态的构建方法中,调用函数-_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,
        );
      },
    );
  }

答案 3 :(得分:0)

@ anmol.majhail我改善了您的calc函数,如下所示:

$ timeout 5 script.sh ??? { echo "Timeout is fine, but this is printed only if script.sh returns an error"; }

与此类似:

已发送3,75 / 30,32 MB