【问题标题】:Flutter get Download Url from Firebase Storage having error using "onComplete" functionFlutter 使用“onComplete”函数从 Firebase 存储中获取下载 URL 时出错
【发布时间】:2020-11-26 05:28:21
【问题描述】:

我正在尝试为我的产品缩略图获取图像。但我在这里使用“onComplete”时遇到问题。我也尝试使用“未来”,但它仍然有同样的问题。 这是我的firebase依赖: firebase_core:^0.5.2 firebase_auth:^0.18.3 cloud_firestore:^0.14.3 firebase_database: ^4.3.0 firebase_storage:^5.0.1 firebase_dynamic_links:^0.5.1

Future<List<String>> uploadProductImages(
      {Map<int, File> imageList, String docID}) async {
    List<String> imagesUrl = List();
    try {
      for (int s = 0; s < imageList.length; s++) {
        Reference storageReference = FirebaseStorage.instance
            .ref()
            .child(appProducts)
            .child(docID)
            .child(docID + "$s.jpg");
        UploadTask uploadTask = storageReference.putFile(imageList[s]);
        Uri downloadUrl = (await uploadTask.onComplete).downloadUrl;
        imagesUrl.add(downloadUrl.toString());
      }
    } on FirebaseException catch (e) {
      imagesUrl.add(error);
      print(e.message);
    }
    return imagesUrl;
  }

【问题讨论】:

  • 能否请您发布您遇到的错误

标签: firebase flutter


【解决方案1】:

您可以从StorageReference 获取网址,即storageReference.getDownloadURL()

     Future<List<String>> uploadProductImages({
    Map<int, File> imageList,
    String docID,
    String appProducts,
  }) async {
    List<String> imagesUrl = List();
    try {
      for (int s = 0; s < imageList.length; s++) {
        StorageReference storageReference = FirebaseStorage.instance
            .ref()
            .child(appProducts)
            .child(docID)
            .child(docID + "$s.jpg");
        final StorageUploadTask uploadTask =
            storageReference.putFile(imageList[s]);
        StorageTaskSnapshot storageTaskSnapshot = await uploadTask.onComplete;
        String downloadUrl = await storageReference.getDownloadURL();
        imagesUrl.add(downloadUrl.toString());
      }
    } on FirebaseException catch (error) {
      imagesUrl.add(error.code);
      print(error.message);
    }
    return imagesUrl;
  }

【讨论】:

    猜你喜欢
    • 2018-07-04
    • 2020-07-15
    • 2018-12-08
    • 2016-09-25
    • 2023-02-08
    • 1970-01-01
    • 2018-02-04
    • 2021-03-22
    • 2016-11-26
    相关资源
    最近更新 更多