【问题标题】:Flutter Firebase Storage 0.5.0 upload file and video errorFlutter Firebase Storage 0.5.0 上传文件和视频报错
【发布时间】:2021-04-16 21:05:22
【问题描述】:

我想做的事:使用 Firebase Storage 0.5.0 上传文件和视频并返回 url。

目前的问题是: 我可以使用 Firebase 存储 0.5.0 上传文件和图像,但我无法返回 url。我还在 Firebase 控制台的 Firebase 存储中看到我上传的文件和视频。

我的代码:

Future<String> _uploadFile(Reference ref, File file,
      [SettableMetadata metadata]) async {
    UploadTask uploadTask = ref.putFile(file, metadata);
    uploadTask.whenComplete(() async {
      try {} catch (onError) {
        print("Error");
      }
    });
    final url = await ref.getDownloadURL();
    return url;
  }

 Future<String> uploadVideo(File video,
      {String refName, SettableMetadata metadata}) async {
    metadata = SettableMetadata(contentType: 'video/mp4');
    final name = refName != null ? refName : path.basename(video.path);
    final ref = _VideoRef.child(name);
    return _uploadFile(ref, video, metadata);
  } 

Future<File> downloadImage(String imageUrl, String savePath) async {
    final ref = _storage.ref(imageUrl);
    var file = File(savePath);
    await ref.writeToFile(file);
    return file;
  }

控制台告诉我的:

FirebaseException (Firebase_storage/object-not-found)。所需引用中不存在任何对象。

我该如何解决这个问题?

【问题讨论】:

    标签: firebase file flutter upload


    【解决方案1】:

    尝试以下方法:

     uploadTask.whenComplete(() async {
          try {
         url = await ref.getDownloadURL();
          } catch (onError) {
            print("Error");
          }
        });
    

    future完成后,调用getDownloadURL()获取url。

    【讨论】:

    • 这不起作用并且应用程序崩溃。 Future _uploadFile(Reference ref, File file, [SettableMetadata metadata]) async { String url = ""; UploadTask uploadTask = ref.putFile(file, metadata); uploadTask.whenComplete(() async { try { url = await ref.getDownloadURL(); } catch (onError) { print("Error"); } });返回网址; }
    • 我收到错误消息“在 null 上调用了 getter '_value'。接收方:null 尝试调用:_value。“DateTime.difference (dart:core-patch/date_patch.dart:201:54 )。但我可以看到我的文件和视频已上传到 Firebase 存储中。
    • 你在哪里使用你的代码中的日期时间类?
    • 上述错误与我给你的代码无关,请添加你使用DateTime类的代码
    • 我在这里使用它: DateTime dateTimeParser(object) { if (object is String) { return DateTime.parse(object); } else if (object is int) { return DateTime.fromMillisecondsSinceEpoch(object); } else { throw "dateTimeParser() 错误对象既不是 String 也不是 int"; } } 当我映射数据时。 Video.fromJson(Map map, {this.id, this.user}) { createdAt = dateTimeParser(map['createdAt']); }
    猜你喜欢
    • 2019-03-10
    • 2019-01-20
    • 2017-07-09
    • 2020-09-07
    • 2017-05-09
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    相关资源
    最近更新 更多