【问题标题】:How to upload pdf to firebase storage with flutter?如何使用颤振将pdf上传到firebase存储?
【发布时间】:2021-04-03 04:21:43
【问题描述】:

我正在使用最新的firebase_storage 版本,现在更新了一些存储方法和引用ploadTask.onComplete 不支持。

那么如何获取上传的 pdf url?

一段代码:

 Future<String> uploadPdfToStorage(File pdfFile) async {
    try {
      Reference ref = FirebaseStorage.instance.ref().child('pdfs/${DateTime.now().millisecondsSinceEpoch}');
    UploadTask uploadTask = ref.putFile(pdfFile, SettableMetadata(contentType: 'pdf')); 
    String downloadUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
  


   final String url = await downloadUrl;


  print("url:$url");
  return  url;
    } catch (e) {
      return null;
    }
  }

【问题讨论】:

  • 你现在的代码有什么问题?如果有错误消息,您应该将其添加到问题中。

标签: firebase flutter dart firebase-storage


【解决方案1】:

这是一个编辑过的代码,应该可以与最新的 firebase storage_package 一起使用:

Future<String> uploadPdfToStorage(File pdfFile) async {
  try {
    Reference ref =
        FirebaseStorage.instance.ref().child('pdfs/${DateTime.now().millisecondsSinceEpoch}');
    UploadTask uploadTask = ref.putFile(pdfFile, SettableMetadata(contentType: 'pdf'));

    TaskSnapshot snapshot = await uploadTask;

    String url = await snapshot.ref.getDownloadURL();

    print("url:$url");
    return url;
  } catch (e) {
    return null;
  }
}

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 2021-04-03
    • 1970-01-01
    • 2019-06-15
    • 2021-04-07
    • 2021-03-07
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多