In this article, we will add Cloud Firestore to a Flutter application, perform different read, write operation and use some queries to retrieve data.

This is the fourth article related to Firebase in Flutter, you can check the previous articles in the below links:

To know how to download the google-service.json file, you can check the first article in the above list. In the last two articles, I created a form using Flutter performed queries for the realtime database and authenticated users with Firebase, but in this article I'm not going to create a form, its mostly going to be code snippet related to Firestore and explaining each one.

Note: You can find the source code for all the Firebase/Flutter tutorials in the following repository: Firebase-Flutter-tutorials. Support me by starring the repository and following me on Github for more awesome content! Let’s get started 😁

Introduction

What is Cloud Firestore?

Both Cloud Firestore and realtime database are nosql database, their are no joins , their are no columns or tables, and you don't have to worry about duplicating your data. The main difference between the two is that Cloud Firestore contains collections and inside these collections you have documents that may contain subcollections or fields mapped to a value while realtime database can be considered as a big json that will contain all the data. The other also important difference to take into consideration is the queries, in realtime database as you can tell from previous articles we can only use one orderByChild().equalTo() (we cannot chain) while in Cloud Firestore as we will see later in the article we can chain queries.

Important Notes To Remember

Queries are shallow , which means if you have a collection, and are retrieving data then you will only get data from the documents under that collection and not from subcollections.

, which means if you have a collection, and are retrieving data then you will only get data from the documents under that collection and not from subcollections. Document size is limited to 1mb

You are charged for every read, write, delete done on a document

Adding Firestore To Flutter

As I said before, to check how to create a flutter project and add the google-service.json file which is used for android, then please check this article How To Use Firebase In Flutter. Next, you need to add the following dependency to the pubspec.yaml file:

dependencies:

cloud_firestore: ^0.13.4+2

Click CTRL + S to save, and you have successfully added Cloud Firestore to your Flutter application!

Adding Data To Firestore

There are two ways to add data to the Cloud Firestore, first way is to specifiy the document name and the second way Cloud Firestore will generate a random id, let us see both cases. So first in your State class you need to get an instance of Cloud Firestore:

Now, you can add the data:

As, you can see in the above, on press of a button, we create a user collection and we use the add() method which will generate a random id. Since the add() method returns Future<DocumentReference> therefore we can use the method then() which will contain a callback to be called when the Future finishes. The variable value which is a parameter passed to the callback is of type DocumentReference therefore we can use the property documentID to retrieve the auto generated id.

As you can see, we added a map, string, int and we can also add an array.