This is a follow-up question to the question asked here. Long story short, I'm trying to limit a Flutter Firebase query only to values contained in a list.
Here's the working code with an additional question at the end. Using the 'shares' collection and adding an ID field to the 'projects' collection as below:
I'm now able to use one Stream to retrieve my shares (per user), and then use that list as a query in a second Stream like so:
@override
Stream<Map<String, dynamic>> getUserSharesStream({@required String uid}) {
return _firestore.collection('shares').doc(uid).snapshots().map((doc) => doc.data());
}
@override
Stream<List<Map>> getListOfProjectsForUser({@required String uid, @required List<String> shares}) {
var ref = _firestore.collection('projects');
return ref
.where('id', whereIn: shares)
.snapshots()
.map((QuerySnapshot snapshot) => snapshot.docs.map((DocumentSnapshot doc) => doc.data()).toList());
}
In the app, I get a list of the projects shared with my user like so:
stream: userSharesQuery,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
Map<String, dynamic> result = snapshot.data;
projectsList = result.keys.toList();
By feeding that list into my projectsListStream.builder
I get a list of the projects shared with my user, which was the answer to the original question.
My remaining question is: apparently I'm limited to 10 items in my query limit list. So how would YOU architect for instances where the number of shared projects is GREATER than 10?
Should I just abandon trying to limit my queries and simply parse the entire list of projects every time, looking for projectIds
that match my user's share list? Or is there a way to call the stream more than once, each time with a list of 10? Or would you recommend an entirely different scheme for accomplishing the task?