Supercharge your Flutter Apps for Android, iOS, and Web with Cloud Firestore Pt. 2 🔥

Joshua de Guzman
Flutter Community
Published in
5 min readMar 18, 2020
Background wallpaper by @aajanita on unsplash.com

I. Introduction

Ever wonder how you can leverage the power of Cloud Firestore in your Flutter app? Please do visit the first article of this series for you to be able to set up the project that we will use.

Flutter Firestore Series 💙 🔥

II. Reading documents from Firestore

a. Preparing the API

Let’s start by defining the methods used for retrieving data from Firestore.

Add the following to your FirestoreApi.

flutter_firestore_crud/lib/services/firestore.dart

snapshots() method allows your app to listen to the latest document changes. If there are changes introduced in your database, Firestore will update the document snapshot and your StreamBuilder, respectively. This is useful for the parts of your app that need to change its state in realtime.

On the other hand, the method getDocuments(), which returns a Future, is used for retrieving documents once per method call. This can be alternatively used if you rarely need to update the data in your app.

In this project, we will use streamCollection() for retrieving our events in the database.

b. Updating the notifier

Next, we need to expose the methods in our API to our notifier. This is where we put most of the business logic in the code.

Add the following code to your events notifier.

flutter_firestore_crud/lib/viewmodel/events_notifier.dart

c. Rendering the UI

Populate the event feeds by adding a StreamBuilder that will consume the document snapshots from your Firestore database.

StreamBuilder is a widget that rebuilds itself based on the latest update on the snapshot of interaction with a Stream. In our case, our StreamBuilder is connected to a Stream exposed from the events notifier.

flutter_firestore_crud/lib/screens/events/widgets/events_list_view.dart
Updating the previously added record in the Firebase console

Awesome! You should now be able to display the list of events stored in Firestore.

Ready for the next challenge?

III. Creating a new document in Firestore

a. Preparing the API

Similar to how we started reading data from Firestore, we need to update our API class first.

Add this function to your FirestoreApi.

flutter_firestore_crud/lib/services/firestore.dart

By default, Firestore can accept a Map object when adding, updating or deleting a document.

Although, we don’t really want to expose too much work on the UI, so we handle the mapping in the model using toJson() method.

flutter_firestore_crud/lib/models/event.dart

b. Updating the notifier

Add the following code to your events notifier.

flutter_firestore_crud/lib/viewmodel/events_notifier.dart

c. Populating the UI

Before we add any new code to our UI, let’s try to understand the Form setup first.

Form for event creation
flutter_firestore_crud/lib/screens/event_form/event_form.dart

_eventFormKey provides access to the Form’s current state in the widget tree. For example, we can easily call the validator callbacks in our form by simply calling _eventFormKey.currentState.validate().

An example of a validator callback would be like this.

flutter_firestore_crud/lib/screens/event_form/event_form.dart

In the code shown above, the error message will show up if the field has no input.

flutter_firestore_crud/lib/screens/event_form/event_form.dart

In the code, _titleTextEditingController is responsible for managing the TextFormField widget of our event title. We can retrieve the input using _titleTextEditingController.text, anywhere in the class.

In the EventForm widget, add the following to the _submitForm() method.

flutter_firestore_crud/lib/screens/event_form/event_form.dart
Adding a new event
Looks like magic? No, it’s just Flutter and Firebase.

IV. Updating and deleting documents in Firestore

a. Preparing the API

Given that we are now dealing with documents that are already in the database, we are required to pass an id parameter, specifying the document we want to update or delete.

Add the following functions to your FirestoreApi.

flutter_firestore_crud/lib/services/firestore.dart

b. Updating the notifier

Add the following code to your events notifier.

flutter_firestore_crud/lib/screens/event_form/event_form.dart

c. Populating the UI

We will be reusing the same widget we used for adding a new document and identify its mode upon loading the screen.

flutter_firestore_crud/lib/screens/event_form/event_form.dart

This is how we open our form in EDIT mode from our event details screen using the EventFormScreenArgs class.

flutter_firestore_crud/lib/screens/event_form/event_form.dart
Updating event details

Next, we need to add the following code to show a delete confirmation prompt to the user.

(Optional) In our project, we will use the flutter_dialogs plugin to display the prompt.

flutter_firestore_crud/lib/screens/event_form/event_form.dart
Displaying a delete confirmation prompt

Lastly, we need to add the following code to request document deletion.

flutter_firestore_crud/lib/screens/event_form/event_form.dart

Amazing! You have just created a Flutter app with Firestore CRUD operations.

We’re done for CRUD operations! Feel free to dance now ~

Stay tuned for next article in this series — where we will discuss the use of Firestore for Flutter Web and start writing complex queries!

V. Resources

For Flutter trainers and organizers, I have written this in a codelab format — please check it out!

If this helped you, please don’t forget to ⭐ the repo. Thank you!

Let’s keep in touch

--

--

Joshua de Guzman
Flutter Community

I'm a software engineer building products in startups, fintech, and marketplaces. I'm here to share some of my thoughts on work and life.