To follow along with the code examples in this book, you should have the following software installed on your Windows, Mac, Linux, or Chrome OS device:
Firebase is a set of tools with which to build scalable applications in the cloud. Among those tools, you'll find authentication, storage, databases, notifications, and hosting.
You can actually choose between two databases: the Firebase Realtime Database and Cloud Firestore. In this chapter, we'll be using the Cloud Firestore database, which is a NoSQL document database that simplifies storing, querying, and updating data in the cloud. More importantly in the context of this book, you can use it as the backend of your iOS and Android apps, with no need to write the code for a web service and, in many cases, without writing any code at all for your server-side service.
Relational databases use tables to store data, with a fixed schema that all records must follow. For example, if you store user data, you can create a users table with three fields: user_id, name, and password. Each record in the table will follow the constraints (rules) you define when you design your table.
In relational databases, you store data in tables. The "columns" of the tables are called fields, and the "rows" of the table are called records. For example, if you store users, the table name might be Users, and the fields might be "Name" and "Surname". "John" - "Doe" and "Bill" - "Smith" are records.
A NoSQL database, on the other hand, is self-describing, so it does not require a schema. All its documents are JSON documents, and, theoretically, each document could have different fields and values (or key-value pairs). In the example we mentioned previously, in the users collection, the first user might have a user_id, name, and password, but the following could also contain a "user_role" field or a "user_age" field. Both documents would still be valid.
Another huge difference is the language used. SQL databases use Structured Query Language to define and manipulate data. This makes SQL databases easy to use and wide spread, but SQL requires you use predefined schemas to design the structure of your data.
In a NoSQL database, you have a dynamic schema and data is unstructured, and can be stored in many different ways.
Generally speaking, when you need to perform complex queries in multiple tables and you have structured data, SQL is the best option. When you don't need to perform queries that require several "JOINS" between tables and you want an easily scalable and fast solution, you would probably choose a NoSQL database.
As an added bonus, a backend created with Firebase can scale over Google server farms, so it's virtually limitless.
The app we'll build in this chapter is an event app where the user will be able to see the program of an event, with the details of the schedule. All data will be hosted remotely, in a Firebase project. The events will be stored in a Cloud Firestore database.
Once authenticated, the user will be able to choose their favorite parts of the event by pressing a star icon. In this way, the "favorites" will be also saved remotely.
The following screenshot shows the app's main screen:
Another interesting aspect of the app is dealing with authentication. This is generally a cumbersome process, but the good news is that dealing with authentication with Firebase and Flutter is rather straightforward. You can see the app's authentication screen in the following screenshot:
Building the project should take about 3 hours.
As mentioned before, Firebase is a set of tools that you can use to build applications in the cloud. As it's a cloud service, you won't need to install any software on your device. Firebase is operated by Google, so you will need a Google account to create your first project.
What are the advantages of using Firebase, instead of following the traditional approach of writing a client-side app, and a server-side (or backend) service?
The tools that are available within Firebase cover most of the services that you would typically have to build yourself, including authentication, databases, and file storage, just to name a few. The client that connects to Firebaseāin our case, a Flutter appāinteracts with these backend services directly, without any middleware server-side service. This means that, when you use the Firestore database, you'll write queries directly in your Flutter app! This is totally different from traditional app development, which usually requires both client and server software to be written, and it's probably the main advantage of using Firebase when developing an app that requires a backend service. You won't need to write, install, or maintain a web service using PHP, or Java, or C#. You'll deal with Firebase directly from your Flutter app.
Every project that involves the use of Firebase begins with the Firebase console. You can reach it at the following address: https://console.firebase.google.com/.
You will be asked to authenticate yourself before accessing the console. In the remote chance that you do not have any Google accounts, you can create one for free from the authentication page. So, let's begin, as follows:
- The container of all services in Firebase is a project. So, we'll begin building our app by creating a new Firebase project.
A Firebase project is the top-level entity for Firebase. Each Firebase feature...