- I am learning flutter/dart and am trying to connect to my firestore database. I want to query a collection for a document then a collection within that document. I'm still learning dart and what I have found is that async stream building should be the best method. I have tried db.instance.collect('').document('').collection('').snapshot() but streambuilder doesn't seem to support this and I get the error 'The method 'collection' isn't defined for the type 'Query'.'. I have to end my query after db.instance.collection('').where('name' isEqualto: 'Name').snapshots() Below is an example. My goal is to query all of the classes and present them in a list.
EX:
Teachers <-- 1st Collection
Teacher Name <--Document within collection
Classes <---Collection 2
Class1 <-- Query This
Class2 <---Query Thisimport '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(), ); } ), ); } }
0
votes
1 Answers
2
votes
In your question, you say that you're using db.instance.collection('').document('').collection('').snapshot(), but in your code, there is no call to document(). Here's what I see:
Firestore.instance
.collection('Teachers')
.where('name', isEqualTo: 'Dr. Who')
.collection('Classes')
.snapshots()
This isn't going to work because where() returns a Query, and Query doesn't have a collection() method. It sounds like what you need to do instead is execute that query, look at the documents in the result set (there could be any number, not just 1), then make anoterh query for each document's subcollections.