Links

Retrieving Data

This page guides you on how to retrieve data from your Firestore collection and document. Here's what we'll cover:

Prerequisites

Before getting started with this section:

Querying a collection

Querying a collection allows you to get a list of documents and show them in your app using scrollable widgets such as ListView, GridView, Column, Row, and StaggeredView.
Firestore Queries on a Column/Row can cause performance issues if the number of documents is >15. In that case, we recommend using a ListView or GridView instead.
To show a list of documents (each representing a single item), let's take an example of a Todo app and build a page with ListView that looks like the below:
Todo list
Querying a Collection and displaying it in UI comprises of the following steps:
  1. 1.
    Designing a page (you can skip this if you already have a page designed)

1. Designing a page

To add a ListView with ListTile widget inside your page:
  1. 1.
    First, drag the ListView widget from the Layout Elements tab (in the Widget Panel) or add it directly from the widget tree.
  2. 2.
    Drag the ListTile widget from the Base Elements tab and drop it inside the ListView.
Designing a page

2. Querying a collection on a ListView

To query a collection on a ListView:
  1. 1.
    Select the ListView widget from the widget tree or the canvas area. Make sure to choose the ListView widget, not the ListTile.
  2. 2.
    Click on the Backend Query tab (on the right side of your screen).
  3. 3.
    Set the Query Type to Query Collection.
  4. 4.
    Scroll down to find the Collection dropdown and set it to your collection.
  5. 5.
    Set the Query Type to List of Documents.
  6. 6.
    (Optional) By default, the result of this query will automatically refresh when the items are added/updated/deleted inside the collection. Checkmark the Single Time Query, to load the result only once and not refresh when any updates are made.
  7. 7.
    Click Save.
Querying a collection on a listView

3. Showing data in UI elements

Filtering a collection query

Sometimes you may need to filter a list based on a condition. For example, you may want to show only incomplete Todo items on the main listing.
To add a filter when querying a collection:
  1. 1.
    Select ListView from the widget tree or the canvas area.
  2. 2.
    Click on the Backend Query tab (on the right side of your screen).
  3. 3.
    Query a collection as per the instructions here. Skip if you have already done so.
  4. 4.
    Scroll down and click on the + Filter button at the bottom
  5. 5.
    Find the Field Name, click on the Unset and select a field on which you would like to apply the filter.
  6. 6.
    Find the Relation dropdown, click on the Unset and choose the relation amongst the list.
  7. 7.
    Find the Value property and set it to an appropriate value.
  8. 8.
    Click Save.
You may choose a Filter relation based on your requirements. For example, to show only incomplete todos, create a Field called isDone, set the Relation to Equal To, and set the Value to False. Here's another example. For showing only users older than 30, create a Field called Age, set the Relation to Greater Than, and set the Value to 30.
Filtering a collection query

Ordering a collection query

You may want to show your list based on a specific order. For example, you could show a Todo list in order of due date.
To specify the order:
  1. 1.
    Select ListView from the widget tree or the canvas area.
  2. 2.
    Click on the Backend Query tab (on the right side of your screen).
  3. 3.
    Query a collection as per the instructions here. Skip if you have already done so.
  4. 4.
    Scroll down and click on the + Order By button at the bottom
  5. 5.
    Find the Field Name, click on the Unset and select the Field which you would like to choose for ordering.
  6. 6.
    Find Order dropdown, click on the Unset and choose the order either Increasing or Decreasing.
  7. 7.
    Click Save.
You may choose the order based on your requirements. For example, to show Todo items in order of due date, you should set Field Name to date and Order to Increasing.
Ordering a collection query

Querying a document

In many cases, you may need to display the details of a single document. Querying a document allows you to get a single document data from inside the collection.
For example, you could display the user's details (who created a Todo item) on another page. The user's details can be fetched from the document based on the record reference.
Showing user's details based on user reference
Querying a Document and displaying it in UI consists of the following steps:
  1. 1.
    Designing a page (you can skip this if you already have a page designed)

1. Designing a page

To design a page:
  1. 1.
    Create/select the page and give it a name.
  2. 2.
    Select the Column widget and set its Padding to 10 and Cross Axis Alignment to Stretch.
  3. 3.
    Add a CircleWidget widget to show the profile picture.
  4. 4.
    Add another Text widget to show the user name.
  5. 5.
    Add one more Text widget to show the user's email.
Create a user details page

2. Querying a document on a page or widget

Passing Data: Make sure to define the parameter on a page that holds data of Type RecordReference (a reference to a single document in a collection). Also, pass the parameter from the previous page. You can follow the instructions for passing the data from one page to another here.
To query a document on a page or widget:
  1. 1.
    Click on the Backend Query tab (on the right side of your screen).
  2. 2.
    Set the Query Type to Document from Reference.
  3. 3.
    Scroll down to find the Collection dropdown and change it to your collection.
  4. 4.
    Set the Source value to a Record Reference (e.g., the name of your parameter that holds a record reference).
  5. 5.
    (Optional) By default, the result of this query will automatically refresh when the document is updated. (e.g., address/phone change) Checkmark the Single Time Query to load the result only once and not refresh when any updates are made.
  6. 6.
    Click Save.
Querying a document on a page

3. Showing data in UI elements

See how to show data in Image and Text widget.

Querying count

It's often useful to know the number of documents that match a certain set of criteria without actually retrieving the matching documents themselves. This is where count queries come in.
The advantage of using count queries over full queries (which return the actual documents) is that they are much faster and more efficient, making them a more practical option for retrieving the number of matching documents in extensive collections.
Let's build an example that shows the unread notifications count for the logged-in user. Here's how it might look:
Querying count
The steps to build such an example are:

1. Query notifications count

To query notifications count:
  1. 1.
    Select the widget or a page and query the collection for which you want to count documents (e.g., notifications).
  2. 2.
    While querying a collection, select the Query Type to Count.
  3. 3.
    You can add a filter to retrieve a specific set of documents (e.g., retrieving notifcation only for the logged user and are unread).
Query notifications count

2. Display notifications count

After querying the notifications count, you can display it on any widget—for example, Badge widget in this case.
To display notifications count:
  1. 1.
    Select the Badge widget, move to the properties panel, click Set from Variable for Text property.
  2. 2.
    From the Set from Variable menu, select the [collection_name] Count (e.g., notifications Count).
Display notifications count

3. Use notifications count to show/hide a widget

You can also use the notifications count to show or hide a widget—for example, show the badge only if counts are greater than zero; otherwise, hide it.
To do that:
  1. 1.
    Navigate to the widget property that allows you to show/hide widget. For example, Show Badge property in this case and click Set from Variable.
  2. 2.
    Set the source to Conditions > Single Condition.
  3. 3.
    Set the First Value to [collection_name] Count (e.g., notifications Count), the operator to Greater Than, and Second Value to 0.
Using notifications count to show/hide a widget

Retrieving List/Array fields

You may have added a Field that stores multiple values of the same data type. For example, a Field named accessory with Field Type set to List and Data Type set to String stores the list of accessories names. Such a Field is called a List/Array Field in FlutterFlow.
Let's build an example that shows the sub-items added inside the Todo. Here's how it looks:
Retrieving List/Array fields demo
The List Field with values added looks like this in FlutterFlow and Firebase:
List/Array Field
Retrieving List/Array Field includes of following steps:

1. Creating a page with a scrollable view (Row/Column/ListView/GridView)

As an example, let's add a ListView with ListTile widget inside your page:
  1. 1.
    First, drag the ListView widget from the Layout Elements tab (in the Widget Panel) or add it directly from the widget tree.
  2. 2.
    Drag the Text widget, drop it inside the ListView, and set its Theme Text Style to Subtitle 2.
Building a page with scrollable view

2. Generating dynamic children

Generating dynamic children help you prepare a list of items present in the List Field. The returned result is stored in a variable and can later be used to populate the ListView. To generate dynamic children:
  1. 1.
    Select ListView from the widget tree or the canvas area.
  2. 2.
    Click on the Generate Dynamic Children tab (on the right side of your screen).
  3. 3.
    Enter the appropriate Variable Name.
  4. 4.
    Set the Source value to your record (A single Firestore document).
  5. 5.
    Under the Available Options, click on Unset and select the array field.
  6. 6.
    Click Save.
Generating dynamic children

3. Showing data in UI elements

To show the items of List Field in the ListTile widget:
  1. 1.
    Select the widget (e.g., Text), navigate to the properties panel, move to a property for which you want to show data, and click Set from Variable.
  2. 2.
    Now select the [variable_name] item from the list. This should be the variable name you specified while Generating Children from Variable.
  3. 3.
    If you see Confirm button, click on it.
Showing data in UI elements