FlutterFlow Docs
Search…
Retrieving Data
This page describes how to retrieve data from your Firestore collection and document.

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 design a page with ListView.
Querying a Collection and displaying it in UI comprises of following steps:
  1. 1.
    Designing a page (you can skip this if you already have a page designed)
  2. 2.
    Querying a Collection on a ListView
  3. 3.
    Showing Data in UI Elements

Designing A Page

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

Querying a Collection on a ListView

To query a collection on a ListView:
  • Select the ListView widget from the widget tree or from the canvas area. Make sure to choose the ListView widget, not the ListTile.
  • Click on the Backend Query tab (on the right side of your screen).
  • Set the Query Type to Query Collection.
  • Scroll down to find the Collection dropdown and change it to your collection.
  • Set the Query Type to List of Documents.
  • (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.
  • Click Save.

Showing Data in UI Elements

To show data in a ListTile:
  • Select the ListTile Widget from the widget tree or from the canvas area.
  • Move to property editor and scroll down to the Title section.
  • Click on the Set from Variable text. (This will open a new panel)
    • Find the Source dropdown, click on unset and select the [collection] Record (from ListView) from the list.
    • Under the Available Options, click on Unset and select the field that you wish to show.
    • Click Save.
  • Scroll down to the Subtitle section.
  • Click on the Set from Variable text. (This will open a new panel)
    • Find the Source dropdown, click on unset and select the [collection] Record (from ListView) from the list.
    • Under the Available Options, click on Unset and select the field that you wish to show.
    • Click Save.
Here's how it looks when you run your app:

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:
  • Select ListView from the widget tree or from the canvas area.
  • Click on the Backend Query tab (on the right side of your screen).
  • Scroll down and click on the + Filter button at the bottom
  • Find the Field Name and click on the Unset and select a field on which you would like to apply the filter.
  • Find Relation dropdown, click on the Unset and choose the relation amongst the list.
  • Find the Value property and set it to an appropriate value.
  • 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.

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:
  • Select ListView from the widget tree or from the canvas area.
  • Click on the Backend Query tab (on the right side of your screen).
  • Scroll down and click on the + Order By button at the bottom
  • Find the Field Name and click on the Unset and select the Field which you would like to choose for ordering.
  • Find Order dropdown, click on the Unset and choose the order either Increasing or Decreasing.
  • 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.

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 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.
Querying a Document and displaying it in UI consists of the following steps:
  1. 1.
    Querying a Document on a Page or Widget
  2. 2.
    Showing Data in UI Elements
First, let's design a page:
  • First, drag the Column widget from the Layout Elements tab (in the Widget Panel) or add it directly from the widget tree. Set its Horizontal Alignment to 0.
  • Add a CircleWidget widget to show the profile picture.
  • Add another Text widget to show the user name.
  • Add one more Text widget to show the user email.

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 page or widget:
  • Click on the Backend Query tab (on the right side of your screen).
  • Set the Query Type to Document from Reference.
  • Scroll down to find the Collection dropdown and change it to your collection.
  • Set the Source value to a Record Reference (e.g. the name of your parameter that holds a record reference).
  • Click Save.

Showing Data in UI Elements

To show data in the Text widget:
  • Select the Text Widget from the widget tree or from the canvas area.
  • Move to property editor and scroll down to the Text property.
  • Click on the Set from Variable text. (This will open a new panel)
    • Find the Source dropdown, click on unset and select the source. For showing data queried on Page, select the [collection] Record (from [pagename]).
    • Under the Available Options, click on Unset and select the field that you wish to show.
    • Click Save.
Here's how it looks when you run your app:

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 that has Field Type set to List and Data Type set to String stores the list of accessories names. Such Field is called List/Array Field in FlutterFlow.
The List Field with values added looks like this in FlutterFlow and Firebase:
Retrieving List/Array Field includes of following steps:
  1. 1.
    Creating a Row/Column/ListView/GridView
  2. 2.
    Generating Dynamic Children
  3. 3.
    Showing Data in UI Elements

Creating a Row/Column/ListView/GridView

As an example, let's add a ListView with ListTile widget inside your page:
  • First, drag the ListView widget from the Layout Elements tab (in the Widget Panel) or add it directly from the widget tree.
  • Drag the ListTile widget from the Base Elements tab and drop it inside the ListView.
  • Select ListTile from the widget tree or from the canvas area.

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 which you can use to populate the ListView. To generate dynamic children:
  • Select ListView from the widget tree or from the canvas area.
  • Click on the Generate Dynamic Children tab (on the right side of your screen).
  • Enter the appropriate Variable Name.
  • Set the Source value to your record (A single Firestore document).
  • Under the Available Options, click on Unset and select the array field.
  • Click Save.

Showing Data in UI Elements

To show the items of List Field in the ListTile widget:
  • Select the ListTile Widget from the widget tree or from the canvas area.
  • Move to property editor and scroll down to the Text property.
  • Click on the Set from Variable text. (This will open a new panel)
    • Find the Source dropdown, click on unset and select the [variable name] [fieldname] (datatype) from the list.
    • Under the Available Options, click on Unset and select the field that you wish to show.
    • Click Save.
Here's how it looks when you run your app:
Last modified 8d ago