Chapters

Hide chapters

Flutter Apprentice

Fourth Edition · Flutter 3.16.9 · Dart 3.2.6 · Android Studio 2023.1.1

Section II: Everything’s a Widget

Section 2: 5 chapters
Show chapters Hide chapters

Section IV: Networking, Persistence & State

Section 4: 6 chapters
Show chapters Hide chapters

16. Firebase Cloud Firestore
Written by Stef Patterson & Kevin D Moore

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

When you want to store information for many people, you can’t realistically store it on one person’s phone. It has to be stored in the cloud. You could hire a team of developers to design and implement a backend system that connects to a database via a set of APIs. But, this could take months. Wouldn’t it be great if you could just connect to an existing system?

This is where Firebase Cloud Firestore comes in. You no longer need to write complicated apps that use thousands of lines of async tasks and threaded processes to simulate reactiveness. With Cloud Firestore, you’ll be up and running in no time.

In this chapter, you’ll add an instant messaging feature to the Yummy app.

While adding this feature, you’ll learn:

  • About Cloud Firestore and when to use it.
  • The steps required to set up a Firebase project with the Cloud Firestore.
  • How to set up user authentication.
  • How to connect to, query and populate the Cloud Firestore.
  • How to use the Cloud Firestore to build your own instant messaging app.

Getting Started

First, open the starter project from this chapter’s project materials and run flutter pub get.

Next, build and run your project. You’ll see the Yummy app’s Chat tab.

Right now, your app doesn’t do much, but when you’re done, you’ll know how to use Cloud Firestore to send and receive messages.

What is Cloud Firestore?

Google has two NoSQL document databases within the Firebase suite of tools: Realtime Database and Cloud Firestore. But what’s the difference?

Setting Up a Firebase Project

Before you can use any of Google’s Cloud services, you have to create a project on the Firebase Console.

Installing Firebase CLI

What is Firebase CLI? It’s a Firebase management toolkit that enables running commands from command-line. Installation varies depending on your platform and preferred installation option — standalone binary or Node Package Manager (npm) that uses Node.js.

Using the Firebase CLI to Log In

To use Firebase from your IDE, you need to log in and select your project. Open Terminal and execute the following:

Adding Firebase

The Firebase team has made things a lot easier for Flutter developers. You used to have to set up iOS, Android, and web apps separately. Now, you can add a Flutter app, and Firebase will do all the work for you.

Adding Authentication

Firebase enables you to add user authentication without having to write and maintain your own server-side code. This can save you a lot of time and effort.

Setting up Firebase Authentication

Return to the Firebase console in the browser. Click the Authentication card.

Understanding Firestore Data Storage

Cloud Firestore stores data in Documents that are like JSON dictionaries in key/value pairs. These pairs are called fields. Documents can also contain nested subcollections and arrays.

Creating Cloud Firestore Database

Return to the Firebase Project Overview page.

Firebase Security Rules

Firebase database security consists of rules that limit who can read and/or write to specific paths. The rules consist of a JSON string in the Rules tab.

Modeling Data

Data modeling is an important part of your app development process. By creating a data model, you can ensure that the data is organized and stored in a way that is efficient, scalable and secure.

Creating User Data Access Object (DAO)

In lib/models, create a new file named user_dao.dart and add the following:

Signing Up

The first task for a user is to create an account. Replace // TODO: Add signup with:

Logging In

Once a user has created an account, they can log in. Replace // TODO: Add login with:

Logging Out

The final feature is log out. Replace // TODO: Add logout with:

Adopting Riverpod

As you saw in Chapter 13, “Managing State”, Riverpod is a great package for providing classes to its children. Your screens need access to these DAO classes. To do that, you’ll create two providers: one for user data and the other for messages.

Creating the Login Screen

To use your app, a user needs to log in. To do that, they need to create an account. You’ll create a dual-use login screen that will allow a user to either log in or sign up for a new account.

Adding a Logout Button

Still in home.dart, replace // TODO: Replace with logout button with:

Adding Message Data Model

Create a new file in the lib/components directory called message.dart. Then, add the following class with date, email, text and reference properties:

Adding Message DAO

Create a new file in lib/models called message_dao.dart. This is your DAO for your messages.

Creating New Messages

Open components/message_list.dart. Replace // TODO: Replace _sendMessage and the line beneath it with your new send message code:

Reactively Displaying Messages

Now that you have a stream of messages, you want to display them.

Key Points

  • Cloud Firestore is a good solution for low-latency database storage.
  • FlutterFire provides an easy way to use Firebase packages.
  • Firebase provides serverless authentication and security through Rules.
  • Creating data access object (DAO) files helps to put Firebase functionalities in one place.
  • Use Firestore to store and retrieve data in real time.
  • You can choose many different types of authentication, from email to other services.

Where to Go From Here?

There are plenty of other Cloud Firestore features that can supercharge your app and give it enterprise-grade features. These include:

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now