如何获取 firebase 集合的文档快照?

时间:2021-04-17 21:05:27

标签: firebase flutter google-cloud-firestore

我试图让用户从 firebase 集合中搜索主题标签。但我对此有点挣扎 这是我如何尝试获取文档快照

class Videos {
  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.commentcount, this.hashtag1, this.hashtag2, this.hashtag3, this.likes, this.previewimage, this.profilepic, this.sharecount, this.uid, this.videourl, this.categorie, this.username});
}
var  videoscollection =
      FirebaseFirestore.instance.collection('videos');

  Videos videosfromsnapshot(DocumentSnapshot snapshot){
    return Videos(
      categorie:snapshot.data()['categorie'],
      commentcount:snapshot.data()['commentcount'],
         hashtag1:snapshot.data()['hashtag1'],
  hashtag2:snapshot.data()['hashtag2'],
   hashtag3:snapshot.data()['hashtag3'],
   likes:snapshot.data()['likes'],
      previewimage:snapshot.data()['previewimage'],
      profilepic:snapshot.data()['profilepic'],
       sharecount:snapshot.data()['sharecount'],
          uid:snapshot.data()['uid'],
      username:snapshot.data()['username'],
   videourl:snapshot.data()['videourl'],

    );
  }

 Stream<Videos> get  provideDocumentFieldStream {

    return videoscollection.doc().snapshots().map(videosfromsnapshot);
       
}

然后我尝试在这里使用它

 searchResults() {
    var showResults = [];
    if (widget.searchinginput.text != "") {
      for (var tripsnapshot in _allResults) {
        var title = DatbaseService.instance
            .videosfromsnapshot(tripsnapshot)
            .hashtag1
            .toLowerCase();
        print(title);
        var title2 = DatbaseService.instance
            .videosfromsnapshot(tripsnapshot)
            .hashtag3
            .toLowerCase();
        print(title);
        var title3 = DatbaseService.instance
            .videosfromsnapshot(tripsnapshot)
            .hashtag2
            .toLowerCase();
        print(title);
        if (title.contains(widget.searchinginput.text.toLowerCase()) ||
            title2.contains(widget.searchinginput.text.toLowerCase()) ||
            title3.contains(widget.searchinginput.text.toLowerCase())) {
          setState(() {
            nosuerfound = true;
          });
          showResults.add(tripsnapshot);
        }
      }
    } else {
      setState(() {
        nosuerfound = true;
      });
      showResults = List.from(_allResults);
    }
    setState(() {
      _resultsList = showResults;
    });
  }

  getusers() async {
    var firestore = FirebaseFirestore.instance;
    QuerySnapshot qn = await firestore.collection('videos').get();

    setState(() {
      _allResults = qn.docs;
    });
    searchResults();
    return "Complete";
  }

所以我尝试的是用户搜索主题标签,每个视频都有 3 个主题标签,当用户输入一个主题标签时,我想显示视频的 3 个主题标签,但它不起作用我认为文档快照应该有问题。 这是我的火力基地

enter image description here

这里是它抛出的地方

class Openmyprofil extends StatefulWidget {
  final TextEditingController searchinginput;

  const Openmyprofil({Key key, this.searchinginput}) : super(key: key);

  @override
  _OpenmyprofilState createState() => _OpenmyprofilState();
}

class _OpenmyprofilState extends State<Openmyprofil> {
  List _allResults = [];
  List _resultsList = [];

  Future resultsLoaded;
  bool nosuerfound = false;
  var titles;
  @override
  void initState() {
    super.initState();
    widget.searchinginput.addListener(_onsearchChanged);
     widget.searchinginput.text;
    setState(() {
      nosuerfound = true;
    });
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    resultsLoaded = getusers();
  }

  _onsearchChanged() {
   
    setState(() {
      nosuerfound = false;
    });
    searchResults();
  }

  

  searchResults() {
    var showResults = [];
    if (widget.searchinginput.text != "") {
      for (var tripsnapshot in _allResults) {
        titles = tripsnapshot;
        var title = DatbaseService.instance
            .userDataFromSnapshot(tripsnapshot)
            .username
            .toLowerCase();
        if (title.contains(widget.searchinginput.text.toLowerCase())) {
          setState(() {
            nosuerfound = true;
          });
          showResults.add(tripsnapshot);
          print(tripsnapshot);
        }
      }
    } else {
      setState(() {
        nosuerfound = true;
      });
      showResults = List.from(_allResults);
    }
    setState(() {
      _resultsList = showResults;
    });
  }

  getusers() async {
    var firestore = FirebaseFirestore.instance;
    QuerySnapshot qn = await firestore.collection('meinprofilsettings').get();
    setState(() {
      _allResults = qn.docs;
    });
    searchResults();
    return "Complete";
  }

1 个答案:

答案 0 :(得分:0)

根据你的评论,首先要做的是改变这个:

commentcount:snapshot.data()['commentcount'],

为此:

commentcount:snapshot.data()['commentcount'].toString(),

这导致了您的错误 int is not a subtype of string,因为 Firebase 中的评论计数是基于您发布的图片的 int。消除此错误,然后我们继续进行,如果其他一切都正确解决,它甚至可以解决您的问题。

shareCount 做同样的事情

sharecount:snapshot.data()['sharecount'].toString()