Creating a Firestore database

In the previous chapter, we've built an app that used a SQL database. The Firestore database is a NoSQL database. These two have very different ways of storing data and change the way you design a storage solution. In Firebase, you have two different database tools: the Cloud Firestore and the Realtime Database. Both are NoSQL databases, but their architecture is rather different. The Cloud Firestore is the most recent, and it's the recommended choice for most new projects as it features a more intuitive data model, with faster queries and enhanced scaling options.

If you want to learn more about the differences between Cloud Firestore and Realtime Database, have a look at the guide available at the following address: https://firebase.google.com/docs/database/rtdb-vs-firestore.

Let's see how to create a Cloud Firestore database first, and then we'll highlight a few tips on how to think about data in a NoSQL database. To create a Cloud Firestore database, perform the following steps:

  1. On the left side of the Firebase Project Overview page, click on the Database link.
  2. From there, under the Cloud Firestore pane, click on the Create Database button.
  1. In the Create database window, choose Start in test mode, as this is the option that allows access to data without authentication, as shown in the following screenshot:

 Later on, we'll also add authentication to the project.

  1. Click Next. You'll be asked to choose among the locations of the Cloud Firestore. Choose one that is close to where you and your users will access data. For example, as I live in Europe, I'll choose one of the europe-west options.
  2. Finally, click Done.

You have now created a Cloud Firestore database, and should see a page like the one shown in the following screenshot:

We'll now insert some data, as follows:

  1. Click on Start collection. A Collection is a container for a set of documents. Call this container event_details, and click Next.
  2. From there, in the Document ID option, click on Auto-ID, then add a few fields and values, as shown in the following screenshot, then click Save:

  1. Repeat the process for another couple of documents, using the same fields and changing the values in line with your preferences.

There are a few rules when dealing with collections and documents in the Cloud Firestore database, as follows:

  • Collections can only contain documents, not other collections, strings, or blobs.
  • Documents must take up less than 1 MB, which is OK for most use cases but you'll need to split the content into several documents when they take more than 1 MB.
  • A document cannot contain another document.
  • A document CAN contain a sub-collection, which can then contain other documents.
  • The Firestore root can only contain collections and not documents.

Now that we have created our Firebase project, a Cloud Firestore database, and inserted some data, it's time to create our Flutter app and integrate Firebase into it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.120.57