我正在尝试从firebase获取一个文档,并将其添加到Videoplayercontroller。但实际上我不知道怎么做。下面是我的代码
var firestore = FirebaseFirestore.instance;
firestore.collection('sendvideos').where("previewimage",isEqualTo: widget.videofile);我尝试的是从firebase获取第一个文档,其中的previewimage字段等于widget.videfile,然后我希望从该文档获取视频资源,以便将其添加到该文档中
controller = VideoPlayerController.network(video)希望任何人能帮助我的火力基地

如您所见,这里有一个预览图像字段,我将该值保存在widget.videofile中。我需要的是
发布于 2021-04-26 04:31:17
您需要先获取文档:
var videoCollectionQuery = await FirebaseFirestore.instance.collection('sendvideos').where("previewimage",isEqualTo: widget.videofile).get();
//this will get all the documents that satisfy your 'where' condition, you need to use'get()' and 'await' also.然后,在您获得文档之后,您可以获得例如第一个文档:
String videoUrl = videoCollectionQuery.docs.isNotEmpty ?videoCollectionQuery.docs[0].data()['videourl'] : 'No results found';
// this will get you the map you need, unless the query is empty, it'll return 'No results found'.我们需要了解Firebase查询是如何工作的。当我们使用Firebase子句时,where总是返回一个docs列表。即使您知道Firebase中只有一个文档,它仍然会以列表的形式返回,列表中只有一个项目。但是要访问这个文档,您仍然需要使用docs[0]。如果没有满足查询的文档,文档将为空,如果对空文档调用docs[0],将抛出null错误。
这就是为什么我们先检查它是否为空,然后再使用docs[0].data()。
发布于 2021-04-26 05:36:27
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final FirebaseUser currentUser = await _firebaseAuth.currentUser();
if (currentUser != null) {
String uidCur = currentUser.uid;
QuerySnapshot querySnapshot = await Firestore.instance
.collection("user_category")
.document(uidCur)
.collection('categories')
.getDocuments();
var x = querySnapshot.documents;
_myCategoryData.clear();
notifyListeners();
x.forEach((f) => _myCategoryData.add(f));MyCategoryData是一个列表
https://stackoverflow.com/questions/67257779
复制相似问题