【问题标题】:How can I get firebase data as Querysnapshot?如何将 Firebase 数据作为 Querysnapshot 获取?
【发布时间】:2021-07-17 00:17:43
【问题描述】:

我试图从 firebase 获取 Array 作为 Flutter 的 Querysnaoshot,但我该怎么做? 这是代码

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"]);

也许我应该映射它或类似的东西? 所以不要像你看到的那样添加它,我想那样使用

 QuerySnapshot snapshots = await firestore.collection('videos').get();
    for (var doc in snapshots.docs) {

....
 _allResults= qn.docs 

但是我该怎么做也许有人可以提供帮助 我没有收到此错误

[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;
    });

【问题讨论】:

  • 据我所知,您的 snapshotsQuerySnapshot。有什么不好的?
  • 好吧,坦率地说,我可能遇到了问题,问题是 data() 是动态的,因为我收到了这个错误 [VERBOSE-2:ui_dart_state.cc(186)] 未处理的异常:类型 'List' 不是 'QueryDocumentSnapshot' 类型的子类型。请检查我的代码,这样你就可以看到错误发生在哪里,也许你现在有任何解决方案。

标签: arrays firebase flutter dart google-cloud-firestore


【解决方案1】:

没有数据有点困难,但我希望这可以帮助你:

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 构造函数。

【讨论】:

  • 这看起来很不错,但我该怎么做 _allResults= allVideoHastags;当 _allResults 是 QueryDoumentsnapshot 列表时。我认为剩下的唯一一个应该可以工作
  • 查看我的帖子我更新它看看最新的更新我需要这样使用它
  • 如果你想要QueryDocumentSnapshot的列表,你可以像_allResults.addAll(doc);这样直接添加它们
  • 你的意思是这样 _allResults= allVideoHastags;如果是这样,那么我试过它说“列表”类型的值不能分配给“列表”类型的变量。因为您的列表 allVideoHastags 是一个字符串列表,所以我只需要它的文档
  • 我已经更新了答案。 _allResults 可以是 QueryDocumentSnapshot 的列表,但你会得到整个文档,而不仅仅是标签。如果这对你来说没问题,它应该可以使用上面的代码。
猜你喜欢
  • 1970-01-01
  • 2020-11-20
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
相关资源
最近更新 更多