Tag Archive firebase

Firebase, Firestore and Flutter

aseemwangoo No Comments

Aspire to inspire…..

Looking back and surfing through my published articles, one amongst them caught my attention…

Firebase, Google-Sign In and Flutter..

This article used the following packages : 

  1. google_sign_in
  2. firebase_auth

What’s New here…

In this article, we will introduce the following :

  1. Cloud Firestore…
  2. Cloud Functions…
Firebase, Firestore and Flutter

As the user clicks on the Sign In with Google,

  1. Google authentication pops up and asks the user credentials..
  2. Once entered, Cloud function triggers..
  3. Key details of the logged in user gets saved in the Cloud Firestore…

What’s Cloud Firestore…

In simple terms, it’s just a No-SQL database which is provided by Firebase, along with RealTime database..

Cloud Firestore / Firestore stores data in documents, which are organized into collections.

Each documentcontains a set of key-value pairs.

All documentsmust be stored in collections. Documents can contain subcollectionsand nested objects, both of which can include primitive fields like strings or complex objects like lists.

Cloud Firestore Structure…

For more details, do check out the link

https://firebase.google.com/docs/firestore/

What’s Cloud Function…

Cloud Functions lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests.

For more details, do check out the link

https://firebase.google.com/docs/functions/

There are mainly 4 important types of triggers :

  1. onCreate — Whenever there is any new entry, either in Authentication / Database….(Firestore)
  2. onUpdate — Triggered when a document already exists and has any value changed.
  3. onDelete — Triggered when a document with data is deleted.
  4. onWrite — Triggered when onCreateonUpdateor onDeleteis triggered..

How to Write Cloud Functions…

You’ll need NodeJS environment and Firebase CLI…

Don’t worry, we got you covered…

For installing Node.js and npm, Node Version Manageris recommended. Once you have Node.js and npm installed, install the Firebase CLI via npm:

npm install -g firebase-tools

firebase login

Go to your Firebase project directory.

firebase init functions.

Select TypeScript / JavaScript

If everything was correct, you will see the following project structure..

Firebase, Firestore and Flutter….

Open index.js and bang!!!!

Below is the link for getting started with Cloud Functions,

https://firebase.google.com/docs/functions/get-started

Storing the Logged in User…

In the index.js file,

you need to

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
const firestore = admin.firestore();

For listening to the creation of users, you only require…

User creation trigger…

Here, createUserAccountis the name of function which will be triggered as User signs in…

The value fetched (email, photoURL and name) gets saved in the Firestore under Users/{userID}

Where Users — Collection and {userID} — document 

Deleting the Logged In user…

Deleting the user…..

Here, deleteUserAccountis the name of function which will be triggered as we delete the user…

Where Users — Collection and {userID} — document

Phew….

Video demonstration :

Complete source code : https://github.com/AseemWangoo/flutter_programs/blob/master/Cloud%20Firestore.zip