Building Chat App in Flutter with Firebase (Latest Updated 2021)
Undoubtedly, chat apps have become a hot topic of the town and have taken the world by storm in recent years. With 2 billion users accessing the WhatsApp messenger on a monthly bases and projected to generate the revenue of 4.8 billion in 2020, it is safe to say that the chat apps will not go anywhere in future.
In addition, here’s what else the current state of chat app market has to reveal:
- Around 2.3 billion users use one of the messaging apps every day but Facebook alone has 2.5 billion monthly users worldwide whereas Instagram has been followed by 1 billion users.
- Monthly active users of the top 3 chat apps including WhatsApp, Facebook Messenger and WeChat have around 4 billion active monthly users.
- According to the survey, the popular chatting apps like Facebook Messenger and WhatsApp are having 73% of users from 18–29 age group, whereas 66% of users are from 30–59 and 43% are 60+.
- The forecast from the report reveals that the number of chat app (WhatsApp) users in the US will jump from 22 million in 2018 to 25.6 million in 2021.
To conclude all these figures and facts, it is worth saying that chat apps have a bright scope in future. So deciding to invest in a chat app is a worthy decision and can accelerate your business growth in future.
Now the central question is “ how you can develop a highly functional chat app and what technologies you can use to create this app?”.
The simple answer to this question is: using Flutter with Firebase!
Before getting started with the steps to build a chat app, you need to clarify what type of chat app you need to create. If you are looking for the fully-functional chat app like WhatsApp, Telegram or WeChat then it is worth to choose Flutter and Firebase to develop a complete chat app right from the scratch as it helps you release on both Google Play Store and iOS App Store. And one of the most common ways to develop a fully-functional chat app is to hire mobile app developer.
But, this blog will also guide you on how to develop high performing chat app in Flutter with Firebase in simple steps…
Let’s begin with the essential points
4 Major Steps to Develop Chat App in Flutter With Firebase
- Firebase Authorization: Sign in, Sign up or Sing Out
- Firebase Firestore for Installing Plugins: Upload, Remove and Retrieve Data Stored in Cloud Firestore
- Creating the Layout of the Chat App Screen
- Final Linking of the Flutter Chat App with Firebase
Let’s understand each point in detail:
Step 1: Firebase Authentication in the App: Sign in, Sign up or Sing Out
This is a most essential part of any chat app to keep your data secure by directly sign-in the app with your username. So here’s what you need to understand to create a Google sign-in.
If you are choosing to proceed with the Google Sign-in, then you can use terminal/CMD or VS code, as both will work as a streamlined code editor and providing support for development operations like debugging, task running and version control.
After that directly jump off all the comments in the main.dart and getting rid of MyHomePage() stateful widget and create a folder inside the library.
Try to create all the screens in this folder with the different names as:
After sorting all the images with certain names, you need to create a widget called Sing-in inside the Sign-in.dart, where you will use scaffold, appBar, body and floating action buttons. In addition, since appBar is going to be very similar for all the screens, so it would be recommended to create it in a separate file, which makes it easier to use it in other screens as well.
Then for adding, use this “import ‘package:flutter/material.dart’;” for importing the files to AppDelegate.h.
After that start the cloud Firestore and storage and then enable sign-in method with Google.
By using the import link, you can easily transfer the files to the app delegate and cut down the risk of manually adding or editing any data at Firebase.
Now you are done with the first step!
Step 2: Installing Widgets To Add Functionality to the App
When it comes to developing a chat app, it is important to integrate the right set of plugins to make it run smoothly.
There are majorly two vital things you need to keep in mind while installing plugins:
- Following the right instructions
- Using the right plugins
Let’s learn it in deep…
Firstly, for installing the plugins, you need to follow these instructions to add “ firebase_auth, google_sign_in, cloud_firestore, firebase_storage” to your chat app. If you find it complicated, then you can hire flutter app developer for the pro assistance.
Secondly, if you are trying to build a chat app that will log in through Google Account, then below are the few plugins you should consider to get user info, real-time data for chatting and uploading an image.
These plugins are:
- Firebase Auth for Flutter: To get started with this plugin, you need to see the documentation.
- Google Sign-In: To access Google Sing-in, you’ll need to register your application for Android integration. But, for iOS integrations, you’ll need to follow these instructions.
- Cloud Firestore Plugin for Flutter: You can easily access this plugin with the help of detailed documentation.
- Firebase Cloud Storage For Flutter: To use this plugin, add firebase_storage but still, in case if you are facing any issue, then it is best to see the directory for a complete sample app using Firebase Cloud Storage.
And here’s another set of plugins that most flutter app development companies usually considered in the chat app:
- Shared_preferences for cache data local
Example For Android Integration
- Cached_Network_image: Here’s how you can install this plugin:
- intl for parse date time
Step 3: Creating The Layout of the Chat App Screens
Since the chat app uses multiple screens including Login Screen, Main Screen, Setting Screen and Chat Screens and each screen is developed with a different set of codes, so we need to understand what instructions you need to follow to develop these screens.
1. Layout of the Login Screen
Once the user successfully sign-in the app, the return variable firebaseUser contains some essential information of the user including displayName, PhotoUrl and more.
Once the user enters the login screen, then you need to verify whether it is a new user or already exists by simply putting a query to the server. In case, if it’s a new user, then you need to write it to the database.
But the job is not ending here! Apart from writing it to the database, you need to write the user info to the local storage and navigate to the main screen when login successfully.
2. Layout the Main Screen
Now, this the main page of your chat app that not just have listed all the users available on the database, but also showcase the complete information including nickname, about me, and avatar. If you want to get into depth and would like to design more fields for the user info, then create a detailed screen to see all particular user info.
Here’s how you can list users by using StreamBuilder. Have a look at the example of codes for further assistance:
In case of when the user touches the back butting at the main screen, the user may exit the app. But, before exiting the app, you need to show a dialogue box at first for the confirmation. And to catch back event, use WillPopScope widget which provides an attribute name onWillPop, This attribute will gonna work first and open the dialogue box whether the user touches on the soft or hardback button.
3. Layout of the Setting Screen
This page will help the user to edit their info like change avatar, nickname and about me a description. You can provide the editing access to additional fields as well if you have created the main page in detail.
So firstly, you need to load current user info from the disk that you wrote at the time of login and show it on UI. Like this:
In case, for example, if a user wants to change the avatar, then you need to upload that particular file to Firebase Storage and then get a file’s URL. After that, new photoURL needs to be updated to Cloud FireStore. Also, keep in mind the name of the file that you have set for the new file. As if you have set the file name with USERID and user uploads a new file, then Firebase will automatically replace the old with the new one.
So much of coding, but you can make the layout task simpler for you by collaborating with the software development company.
4. Layout of the Chat Screen
Now the chat screen is where user can send text and image message to a friend and vice versa. So it means chat screen will be handling multiple things.
Here’s how you can handle it:
Firstly, you need to layout the root widgets as like below coding:
The root is WillPopScope because you need to handle the back press. In case, the sticker or keyboard is shown, then you need to hide it instead of navigating it to the previous screen.
Secondly, sending stickers in the chat is one of the most common things that users usually use in the chat app. But when the sticker appears, the keyboard usually disappears. So you need a variable isShowSticker to detect whether the app should show the sticker or not. In addition, focusNode will help you control your focus on the text input.
Thirdly, to load previous messages and listening to new incoming messages, it is recommended to use StreamBuilder, like this:
Fourthly, how to handle the message load? If the user sends an image, you simply need to upload that image and get the URL, then just add the content of the message along with the URL. When the app will receive a new message, firstly you need to check the type of message. If it is an image, then you need to load this image from the network and if this is a type of sticker, then show it from the local file.
4. Linking The Flutter Chat App With Firebase
After a successful layout of various screens, you are finally done with the final structure using Google Firebase firestore.
In this article, we have learned how to develop a chat app in Flutter with Firebase in the simple steps. However, navigating these steps practically might be not as simple as it may sound in reading. If you are a beginner, then you may face multiple bugs and issues, but don’t panic. The simplest way to resolve the issue is to hire mobile app development company as they are backed by highly trained developers that not only understand your business needs but also suggest you optimum solutions