如何访问 firebase 中的文档?

时间:2021-04-09 15:07:40

标签: firebase flutter google-cloud-firestore

在我的项目中,我试图获取视频的 id,然后在我的视频中打开视频,但我在 3 分中挣扎

  1. 如何将真实 ID 保存到 firebase 中? 这就是我正确做事的方式 知道
uploadVideo() async {
    setState(() {
      isuploading = true;
    });
    try {
      var firebaseuseruid = FirebaseAuth.instance.currentUser.uid;
      DocumentSnapshot userdoc = await FirebaseFirestore.instance
          .collection('meinprofilsettings')
          .doc(firebaseuseruid)
          .get();
      var alldocs = await FirebaseFirestore.instance.collection('videos').get();
      int length = alldocs.docs.length;
      String videourl = await uploadvideotostorage("Video $length");
      String previewimage = await uploadimagetostorage("Video $length");
      FirebaseFirestore.instance.collection('videos').doc("Video $length").set({
        'username': userdoc.data()['username'],
        'uid': firebaseuseruid,
        'profilepic': userdoc.data()['url'],
        'id':"Video $length",
        'likes': [],
        'commentcount': 0,
        'sharecount': 0,
        'hashtag1': hashtagcontroller.text,
        'hashtag2': hashtagcontroller2.text,
        'hashtag3': hashtagcontroller3.text,
        'videourl': videourl,
        'previewimage': previewimage,
        'ratings': [],

      });
      Navigator.pop(context);
    } catch (e) {
      print(e.toString());
    }
  }
}


我想要的是视频的真实 id 而不是“视频 $length” 2. 如何在创建时访问 id,例如如何调用它? ? 3.我怎样才能用这个id创建一个新的集合,然后像这样保存一些数据,但是像这样 new collection=> videoed => new field with some data , new field with some data

这是我上传它的方式 图片为预览图片

getpreviewimage() async {
    final previewimage = await flutterVideoCompress.getThumbnailWithFile(
      widget.videopath_asstring,
    );
    return previewimage;
  }

  compressvideo() async {
    if (widget.imageSource == ImageSource.gallery) {
      return widget.videofile;
    } else {
      final compressvideo = await flutterVideoCompress.compressVideo(
          widget.videopath_asstring,
          quality: VideoQuality.MediumQuality);
      return File(compressvideo.path);
    }
  }

  uploadvideotostorage(String id) async {
    final video = await allvideos.child(id).putFile(await compressvideo());
    String url = await video.ref.getDownloadURL();
    return url;
  }

  uploadimagetostorage(String id) async {
    final video = await allimages.child(id).putFile(await getpreviewimage());
    String url = await video.ref.getDownloadURL();
    id=url;
    return url;
  }

1 个答案:

答案 0 :(得分:0)

如果我理解正确,请将它们更改为如下所示:

 Future<String> uploadvideotostorage(String id) async {
    final video = await allvideos.child(id).putFile(await compressvideo());
    String url = await video.ref.getDownloadURL();
    return url;
  }

 Future<String> uploadimagetostorage(String id) async {
    final video = await allimages.child(id).putFile(await getpreviewimage());
    String url = await video.ref.getDownloadURL();
    return url;
  }
  • 还有这个:

    String videourl = await uploadvideotostorage(firebaseuseruid);
    

'id':videourl,

这是假设 compressvideo() 工作正常。