如何获取Firebase数据作为Querysnapshot? [英] How can I get firebase data as Querysnapshot?
问题描述
我正在尝试从firebase中获取Array作为来自flutter的Querysnaoshot,但是我该怎么做呢?这是代码
Im trying to get Array from firebase as a Querysnaoshot from flutter but how can I do that ? This is the code
class Videos {
final String allhashtagsofeveryvideo;
final String categorie;
final String commentcount;
final String hashtag1;
final String hashtag2;
final String hashtag3;
final String likes;
final String previewimage;
final String profilepic;
final String sharecount;
final String uid;
final String username;
final String videourl;
Videos( {this.allhashtagsofeveryvideo,this.commentcount, this.hashtag1, this.hashtag2, this.hashtag3, this.likes, this.previewimage, this.profilepic, this.sharecount, this.uid, this.videourl, this.categorie, this.username});
}
Videos videosfromsnapshot(DocumentSnapshot snapshot) {
return Videos(
categorie: snapshot.data()['categorie'],
commentcount: snapshot.data()['commentcount'].toString(),
hashtag1: snapshot.data()['hashtag1'],
hashtag2: snapshot.data()['hashtag2'],
hashtag3: snapshot.data()['hashtag3'],
likes:snapshot.data()['likes'].length.toString(),
previewimage: snapshot.data()['previewimage'],
profilepic: snapshot.data()['profilepic'],
sharecount: snapshot.data()['sharecount'].toString(),
uid: snapshot.data()['uid'],
allhashtagsofeveryvideo:snapshot.data()['Hashtagsforallvideos'],
username: snapshot.data()['username'],
videourl: snapshot.data()['videourl'],
);
}
var firestore = FirebaseFirestore.instance;
List<QueryDocumentSnapshot> _allResults = [];
QuerySnapshot snapshots = await firestore.collection('videos').get();
for (var doc in snapshots.docs) {
_allResults.addAll(doc.data()["Hashtagsforallvideos"]);
也许我应该在它上面绘制地图或类似的东西?因此,与其像您看到的那样添加它,不如我想那样使用
Maybe I should map over it or something like that ? So instead of adding it like you seeing I wanna use like that
QuerySnapshot snapshots = await firestore.collection('videos').get();
for (var doc in snapshots.docs) {
....
_allResults= qn.docs
但是我该怎么做,也许有人可以帮忙不,我收到此错误
But how can I do this maybe anyone can help No im getting this error
[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'QueryDocumentSnapshot'
#0 _OpenallsinglehashtagsState.getusers
package:wichtigdenyady/homesearchingall/openalldocs.dart:90
<asynchronous suspension>
那一行是这个
_allResults.addAll(doc.data()["Hashtagsforallvideos"]);
大概是这样的
var firestore = FirebaseFirestore.instance;
QuerySnapshot qn = await firestore.collection('videos').get();
if (!mounted) return;
setState(() {
_allResults = qn.docs;
});
推荐答案
取消数据有点困难,但我希望这对您有帮助:
It's a little bit hard withot the data but I hope this can help you:
Future<List<QueryDocumentSnapshot>> getVideos() async {
List<String> allVideoHastags = [];
List<QueryDocumentSnapshot> _allResults = [];
QuerySnapshot snapshots = await _firestore.collection('videos').get();
for (QueryDocumentSnapshot videoSnapshot in snapshots.docs) {
List<String> videoHastags =
List.from(videoSnapshot.data()['Hashtagsforallvideos']);
allVideoHastags.addAll(videoHastags);
_allResults.add(videoSnapshot);
}
return _allResults;
}
在大多数情况下,当我们使用 Firestore
数组并在 List
中需要它们时,我们会错过 List.from
构造函数.
In most cases we miss the List.from
constructor when we work with Firestore
arrays and need them in a List
.
这篇关于如何获取Firebase数据作为Querysnapshot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!