How to Loop Through an Array of Document References in Firestore and Add Them to a Page State in FlutterFlow?

Database & APIs

I'm working with a Firestore structure where I have a Restaurant collection, and within that, an Orders subcollection. Each order document contains a cartlist field, which is an array of document references pointing to items in the Menu subcollection.

I'm trying to loop through all the Orders in FlutterFlow, then within each order, loop through the cartlist array and add each document reference from the cartlist to a page state variable (like ordersList).

So far, I've set up the outer loop to go through the Orders and increment an ordersLoopIndex. Inside that, I need a nested loop to go through each cartlist, but I'm struggling with the exact steps to properly add each document reference to my ordersList page state.

What is the best way to handle this kind of nested loop for an array of document references in Firestore, and how do I store these references in a page state without using filtering or sorting options? Any help would be appreciated!

What have you tried so far?

What I've Tried So Far:

  1. Firestore Query for Orders:

    • I successfully queried the Orders subcollection and used a loop (ordersLoopIndex) to iterate through each order.

  2. Accessing cartlist:

    • Inside the loop, I accessed the cartlist field from each order document. The cartlist is an array of document references pointing to Menu items.

  3. Attempted Nested Loop:

    • I tried setting up a nested loop inside the outer loop to go through the cartlist items using an index (e.g., cartlistIndex), but I struggled to properly add each reference from the cartlist into a page state (ordersList).

  4. Using "Add to List" for Page State:

    • I used the Update Page State action to add to ordersList, but when selecting cartlist, it prompted me to choose options like "Filter List Items", "Unique List Items", etc. I tried using Unique List Items but was unsure what to select for the Unique Key.

Did you check FlutterFlow's Documentation for this topic?
Yes
2
1 reply