gpt4 book ai didi

flutter - Firestore查询文档的子集合

转载 作者:行者123 更新时间:2023-12-03 03:22:57 24 4
gpt4 key购买 nike

  • 我正在学习 flutter / Dart ,并试图连接到我的Firestore
    数据库。我想查询一个文档的集合,然后
    该文档中的集合。我还在学习 Dart ,我在学什么
    已经发现,异步流构建应该是最好的方法。
    我努力了
    db.instance.collect('')。document('')。collection('')。snapshot()但
    streambuilder似乎不支持此功能,并且出现错误“
    没有为“查询”类型定义“收集”方法。我必须
    在db.instance.collection('')。where('name'isEqualto:
    'Name')。snapshots()下面是一个示例。我的目标是查询所有
    上课,并在列表中列出。
  • EX:教师<-1st
    馆藏教师姓名<-馆藏内文件
    类<-集合2 Class1 <-查询此Class2 <-查询
    这个
       import 'package:flutter/material.dart';
    import 'package:cloud_firestore/cloud_firestore.dart';

    class ClassListPage extends StatelessWidget{
    @override


    Widget build(BuildContext context){
    return Material(
    child: new StreamBuilder<QuerySnapshot>(
    stream: Firestore.instance.collection('Teachers').
    where('name', isEqualTo: 'Dr. Who')
    //.collection('Classes') <--This is not working
    .snapshots(),
    //HERE is where I want to query for the 'classes' collection then query
    //for the documents within
    builder:(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
    if(!snapshot.hasData)return new Text('..Loading');
    return new ListView(
    children: snapshot.data.documents.map((document){
    return new ListTile(
    title: new ListTile(
    title: new Text(document['name']),

    ),
    );

    }).toList(),
    );
    }

    ),
    );

    }
    }
  • 最佳答案

    在您的问题中,您说您正在使用db.instance.collection('').document('').collection('').snapshot(),但是在您的代码中,没有调用document()。这是我看到的:

    Firestore.instance
    .collection('Teachers')
    .where('name', isEqualTo: 'Dr. Who')
    .collection('Classes')
    .snapshots()

    这是行不通的,因为 where()返回一个Query,并且Query没有 collection()方法。听起来您需要做的是执行该查询,查看结果集中的文档(可以有任何数字,而不仅仅是1),然后对每个文档的子集合进行另一个查询。

    关于flutter - Firestore查询文档的子集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61196359/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com