FlutterFlow Docs
Search…
⌃K

Retrieving Data

This page describes how to retrieve data from your Firestore collection and document.
Retrieving Data from the Firestore collection and document includes:

Prerequisites

Before getting started with this section:

Querying a collection

Querying a collection allows you to get a list of documents from inside the collection and show them in your app (e.g., in a ListView or Column widget).
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.

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

Retrieving Custom DataType fields

You might have added a Field that stores a custom data type. For example, address field that stores apartment, street, city, country, zip code, etc., as subfields. Such a field has its Data Type set to Data Type and Data set to a custom data type name.
A sample custom data type field named address looks like this:
Address DataType field
Let's build an example that shows all the subfields such as apartment, street, city, country, and zip code of the custom datatype field named address into TextFields of a page. Here's how it looks:
Showing custom datatype subfields
Retrieving Custom DataType fields comprises the following steps:

1. Building a page

To build a page with address fields:
  1. 1.
    Create/select a page.
  2. 2.
    Select the Column and add a TextField to capture the address details and give it a name.
  3. 3.
    Similarly, add more TextField widgets as per subfields inside the custom data type field.
Building a page

2. Showing data in UI elements

To show the custom data type subfields in the TextField widget:
  1. 1.
    Select TextField from the widget tree or the canvas area.
  2. 2.
    Move to the Property panel and scroll down to the TextField Properties section.
  3. 3.
    Find the Initial Value property and click on the Set from Variable text. (This will open a popup on the left side)
    1. 1.
      Here you can select a Document. To show the address of the logged-in user, select Authenticate User -> FieldName (e.g., address field with custom data type as Address).
    2. 2.
      Select Field that you want to display in this TextField.
    3. 3.
      Click Confirm.
    4. 4.
      Similarly, set other subfields data into TextField.
Showing data in UI elements