如何在firebase中循环阵列?

如何在firebase中循环阵列?,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我正在尝试从firebase获取数据。但我有点挣扎,这就是现在的样子 getusers() async { var firestore = FirebaseFirestore.instance; List listOfIds = []; QuerySnapshot qn= await firestore .collection('videos') .get() .then((QuerySnapshot querySnapsh

我正在尝试从firebase获取数据。但我有点挣扎,这就是现在的样子

getusers() async {
    var firestore = FirebaseFirestore.instance;
    List listOfIds = [];

  QuerySnapshot qn=  await firestore
        .collection('videos')
        .get()
        .then((QuerySnapshot querySnapshot) {
      querySnapshot.docs.forEach((doc) {
        setState(() {
         
         
        });
      });
    });

    if (!mounted) return;
    _allResults =qn.docs;
我想要的是获取hashtasg数组字段,然后将其添加到_allresults中的qn.doc数据中。但是我怎么能做到呢? 这是我的火力基地,你可以看看它的样子

最后一步,我想在howle标签数组上循环

这是我的小部件

 class Openalldocs extends StatefulWidget {
  final TextEditingController searchinginput;
  static const route = '/openalldocs';

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

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

class _OpenalldocsState extends State<Openalldocs> {
  List _allResults = [];
  List _resultsList = [];
  Future resultsLoaded;
  bool nosuerfound = false;
  String searchresult;

  @override
  void initState() {
    super.initState();
    widget.searchinginput.addListener(_onsearchChanged);
    setState(() {
      nosuerfound = true;
    });
  }

  @override
  void dispose() {
    widget.searchinginput.removeListener(_onsearchChanged());

    super.dispose();
  }

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

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

  searchResults() {
    var showResults = [];
    if (widget.searchinginput.text != "") {
      for (var tripsnapshot in _allResults) {
        var title = DatbaseService.instance
            .videosfromsnapshot(tripsnapshot)
            .hashtag1
            .toLowerCase();
        var title2 = DatbaseService.instance
            .videosfromsnapshot(tripsnapshot)
            .hashtag2
            .toLowerCase();
        var title3 = DatbaseService.instance
            .videosfromsnapshot(tripsnapshot)
            .hashtag3
            .toLowerCase();
        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;
    List listOfIds = [];
 QuerySnapshot qn=  await firestore
        .collection('videos')
        .get()
        .then((QuerySnapshot querySnapshot) {
      querySnapshot.docs.forEach((doc) {
        setState(() {
                       _allResults.add(doc.data()["hashtag1"]);         

         
        });
      });
    });

    if (!mounted) return;

    searchResults();
    return "Complete";
  }

  @override
  Widget build(BuildContext context) {
    final user = Provider.of<Userforid>(context);
    if (nosuerfound == true) {
      return ListView.builder(
          itemCount: _resultsList.length,
          itemBuilder: (BuildContext context, int index) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // the AMOUNT is how many hashtags you want to show
                for (var i = 0; i < _resultsList.length; i += 1) ...[
                  // the SizedBox will only exist between the elements in the list
                  // as before
                  if (i != 0) SizedBox(height: 6),
                  // create a builder to allow declaring a variable
                  Builder(
                    builder: (context) {
                      // declare the hashtag variable
                      final hashtag = 'hashtag${i + 1}';

                      return InkWell(
                        onTap: () {
                          // do something with the hashtag stored in the variable
                          // this will make it relative to the element in the list
                        },
                        child: Column(
                          children: <Widget>[
                            // why is there a Column inside another with only one child?
                            // I would recommend to remove it
                            Column(
                              children: [
                                HighlightedMatchesText(
                                  searchString: widget.searchinginput.text,
                                  // notice how I am using the hashtag variable here
                                  // instead of a constant? ('hashtag1'), by the way
                                  // the for loop will make the hashtag start at 0
                                  // you can change it by increment in the declaration
                                  // `final hashtag = 'hashtag${i+1}'`, if you want
                                  // the existing behavior
                                  content: _resultsList[index][hashtag],
                                ),
                              ],
                            ),
                            // what is this? if it is to add more space between the items
                            // in the list, I recommend removing it from here, and add it
                            // to the first `SizedBox` in the for loop
                            // in case you do that, the Column that this widget belong
                            // would also only now contain one widget, so, there is no
                            // need to have it
                            SizedBox(height: 3),
                          ],
               
       
类Openalldocs扩展StatefulWidget{
最终文本编辑控制器搜索输入;
静态常量路由='/openalldocs';
const Openalldocs({Key-Key,this.searchinginput}):super(Key:Key);
@凌驾
_OpenalldocsState createState();
}
类_OpenalldocsState扩展状态{
列出所有结果=[];
列表_resultsList=[];
未来结果加载;
bool nosuerfound=false;
字符串搜索结果;
@凌驾
void initState(){
super.initState();
widget.searchinginput.addListener(_onsearchChanged);
设置状态(){
nosuerfound=true;
});
}
@凌驾
无效处置(){
widget.searchinginput.removeListener(_onsearchChanged());
super.dispose();
}
@凌驾
void didChangeDependencies(){
widget.searchingput.text;
resultsLoaded=getusers();
super.didChangeDependencies();
}
_onsearchChanged(){
设置状态(){
nosuerfound=false;
});
搜索结果();
}
搜索结果(){
var showResults=[];
如果(widget.searchingput.text!=“”){
for(所有结果中的变量tripsnapshot){
var title=DatbaseService.instance
.videosfromsnapshot(tripsnapshot)
.hashtag1
.toLowerCase();
var title2=DatbaseService.instance
.videosfromsnapshot(tripsnapshot)
.hashtag2
.toLowerCase();
var title3=DatbaseService.instance
.videosfromsnapshot(tripsnapshot)
.hashtag3
.toLowerCase();
if(title.contains(widget.searchinginput.text.toLowerCase())||
title2.contains(widget.searchinginput.text.toLowerCase())||
标题3.contains(widget.searchinginput.text.toLowerCase()){
设置状态(){
nosuerfound=true;
});
showResults.add(tripsnapshot);
}
}
}否则{
设置状态(){
nosuerfound=true;
});
showResults=List.from(_allResults);
}
设置状态(){
_结果列表=显示结果;
});
}
getusers()异步{
var firestore=FirebaseFirestore.instance;
列表listOfIds=[];
QuerySnapshot qn=等待firestore
.collection(“视频”)
.get()
.然后((QuerySnapshot QuerySnapshot){
querySnapshot.docs.forEach((doc){
设置状态(){
_添加(doc.data()[“hashtag1”]);
});
});
});
如果(!已安装)返回;
搜索结果();
返回“完成”;
}
@凌驾
小部件构建(构建上下文){
最终用户=提供者(上下文);
if(nosuerfound==true){
返回ListView.builder(
itemCount:\u resultsList.length,
itemBuilder:(构建上下文,int索引){
返回列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
//数量是要显示的哈希标记数
对于(变量i=0;i<_resultsList.length;i+=1)[
//SizedBox将仅存在于列表中的元素之间
//一如既往
如果(i!=0)SizedBox(高度:6),
//创建一个生成器以允许声明变量
建筑商(
生成器:(上下文){
//声明hashtag变量
final hashtag='hashtag${i+1}';
回墨槽(
onTap:(){
//对存储在变量中的hashtag执行一些操作
//这将使其相对于列表中的元素
},
子:列(
儿童:[
//为什么在另一个只有一个孩子的专栏里有一个专栏?
//我建议将其删除
纵队(
儿童:[
HighlightedMatchesText(
searchString:widget.searchinginput.text,
//注意我在这里是如何使用hashtag变量的
//而不是常数('hashtag1'),顺便说一下
//for循环将使hashtag从0开始
//您可以通过声明中的增量来更改它
//'final hashtag='hashtag${i+1}'`,如果需要的话
//现有行为
内容:_resultsList[索引][标签],
),
],
),
//这是什么?如果要在项目之间添加更多空间
//在列表中,我建议从这里删除它,然后添加它
//到for循环中的第一个'SizedBox'
//如果您这样做,这个小部件所属的列
//现在也只包含一个小部件,所以没有
//需要它吗
尺寸箱(高度:3),
],
更新 这一行表示_resultList是一个文档列表,您希望从中访问所有的hashtag,因为您有for循环,该循环一直持续到_re的长度
content: _resultsList[index].data()[hashtag],
content: _resultsList[index].data()["hashtag1"],
.then((QuerySnapshot querySnapshot) {
  _allResults = querySnapshot.docs;
}
Future<String> getusers() async {
    var firestore = FirebaseFirestore.instance;
    List listOfIds = [];
 QuerySnapshot qn=  await firestore
        .collection('videos')
        .get();
for (var doc in qn.docs) {
   setState(() {
                       _allResults.add(doc.data()["hashtag1"]);
        });
}
      });
    });

    if (!mounted) return "Error loading";

    searchResults();
    return "Complete";
  }
 List<QueryDocumentSnapshot> _allResults =[]
    
    QuerySnapshot qn = await firestore.collection('videos').get();
        if (!mounted) return;
    
        setState(() {
          _allResults = qn.docs;
        });