Book a free call

How to Build a Chat App in Flutter with Firebase

Flutter builds the chat screens while Firebase handles accounts, live messages and push alerts, so a small team can ship a chat app without a server.

Hertiv engineering teamReviewed by a senior engineer
7 min read
How to Build a Chat App in Flutter with Firebase
On this page

Short answer

A chat app in Flutter with Firebase is a mobile app whose screens are built with Google's Flutter toolkit while Google's Firebase platform handles accounts, live message storage, file uploads and push notifications. The combination lets a small team ship a working chat app without running its own server.

What do Flutter and Firebase give a chat app?

A chat app in Flutter with Firebase is a mobile app whose screens are built with Google's Flutter toolkit while Google's Firebase platform handles accounts, live message storage, file uploads and push notifications. Flutter is a UI toolkit that builds one codebase for Android and iOS, and Firebase is a set of hosted services that store data and send alerts.

The split is the point. Flutter owns what the user sees: the message list, the input box, the chat rooms and the settings screens. Firebase owns what happens behind the scenes: who is signed in, where messages live, which devices get a push alert, and where photos are stored. Neither side needs a server that the team runs and maintains.

This fits teams that need to ship fast and learn from real users. A single developer can build the first version, and the hosted services scale with the user count without a rewrite.

The trade-off is dependence on Google's platform and pricing, so a team should read the Firebase pricing page and write "see vendor site" where a figure cannot be confirmed, rather than guessing costs. The wider Flutter picture is covered in the guide to what Flutter is.

How does a Flutter and Firebase chat app fit together?

A Flutter and Firebase chat app fits together as a set of Firebase services behind a Flutter front end, with each service doing one job and the app talking to them through the FlutterFire plugins.

Part of the appFirebase serviceWhat it does
Sign-inFirebase AuthEmail, phone or social sign-in, and user identity
MessagesCloud FirestoreLive message documents, synced to every device
Photos and filesCloud StorageUploads and download links for media
Push alertsCloud MessagingNotifies a user when a new message arrives
Server logicCloud FunctionsModeration, notifications and cleanup jobs
Crash and use dataCrashlytics and AnalyticsCrash reports and usage counts

The message flow is simple. A user writes a message, the app saves it as a document in Firestore, and every device listening to that chat room receives it within a second. A Cloud Function then sends a push alert to the other user through Cloud Messaging, so the chat works even when the app is closed.

How do you set up the project?

You set up the project by creating a Firebase project, connecting it to the Flutter app with the FlutterFire command line tools, and adding the plugins for Auth, Firestore, Messaging and Storage.

  1. Create the Firebase project. In the Firebase console, create a project and register an Android app and an iOS app with the right package names.
  2. Install the FlutterFire tools. Add the FlutterFire command line tool, run the configure step, and let it write the platform files for both apps.
  3. Add the plugins. Add firebase_core, firebase_auth, cloud_firestore, firebase_messaging and firebase_storage to the app's dependencies.
  4. Turn on Auth. Enable the sign-in methods the app needs, such as email and phone, and set the rules for who can create an account.
  5. Shape the Firestore database. Create the collections for users, chats and messages, and write the security rules that limit who can read and write each one.
  6. Wire up push alerts. Request notification permission, store each device's token in Firestore, and test a message to a closed app.

The step teams skip most often is the security rules. Firestore starts locked down or wide open depending on the setup, and neither default is right for a chat app. Write the rules before the first test user signs in, or private messages may be readable by anyone with the app. A general guide to building Flutter apps is in how to build a native app with Flutter.

How do you model messages and chats in Firestore?

You model messages and chats in Firestore with a collection of chat rooms, each holding a subcollection of message documents, so the app can listen to one room at a time and load messages in pages.

A common shape is a chats collection, where each document holds the members, the last message and the last activity time. Inside each chat document sits a messages subcollection, where each document holds the sender, the text, the send time and the message type.

Message types include text, image and system note. The app listens to the messages subcollection in time order and shows the newest at the bottom.

Keep each message document small and flat. A message that embeds the sender's full profile will go stale when the profile changes, so store only the sender's id and fetch the profile separately.

For one-to-one chats, use a chat id built from the two user ids in sorted order, so the same pair always finds the same room. For group chats, store the member list on the chat document and check it in the security rules, so only members can read the room.

How do you show messages live and send new ones?

You show messages live by listening to the room's message query with a stream, so new documents appear in the list without a refresh, and you send a message by adding a document to the same collection.

In Flutter, a StreamBuilder listens to the messages query ordered by send time, limited to the latest 50, and rebuilds the list whenever Firestore sends a change. The list scrolls to the newest message when the user is already at the bottom, and stays put when the user has scrolled up to read older messages, which is the behaviour users expect.

Sending is a single write: the app adds a document with the sender id, the text and a server timestamp, then clears the input box at once so the app feels instant.

The server timestamp matters, because device clocks disagree, and ordering by a device clock puts messages in the wrong order. Optimistic display, where the message appears before the write confirms, makes the app feel fast, but the app must mark the message as failed if the write does not go through.

How do you add push notifications and media?

You add push notifications with Cloud Messaging and a small Cloud Function that fires when a new message document is created, and you add media with Cloud Storage uploads linked from the message document.

For notifications, the app stores each device's messaging token in the user's document. When a message is written, the Cloud Function reads the chat's members, skips the sender, and sends a notification to the other members' tokens with the sender's name and a short preview. The function should skip users who are already looking at the chat, which the app can record with a presence flag.

For media, the app uploads the photo or file to Cloud Storage, gets a download link, and saves a message document with the type set to image and the link in a field.

The app shows a thumbnail while the upload runs and replaces it with the real image once the link is saved. Keep uploads small on mobile networks by resizing images before upload, and set Storage rules so only chat members can read the files.

Costs depend on storage and traffic, so check the Firebase pricing page rather than guessing. The cost picture for Flutter work generally is covered in the guide to Flutter app development cost.

Example (hypothetical): a clinic support chat estimates its data

Example (hypothetical): a small clinic builds a support chat where patients message the front desk. It expects 2,000 daily users, and each user sends about 15 messages a day, which is 2,000 times 15, or 30,000 messages a day.

Each message document is about 1 kilobyte, so one day of messages is 30,000 times 1 kilobyte, or about 30 megabytes. Over 30 days that is 30 megabytes times 30, or about 900 megabytes of new message data a month, before media. That is small enough for the team to stop worrying about storage and focus on the chat experience.

Reads are the bigger number. Each user opens the chat 3 times a day and each open loads 50 messages, which is 2,000 users times 3 opens times 50 messages, or 300,000 document reads a day.

The team decides to page messages in batches of 25 instead of 50, which halves the reads to 150,000 a day, and to cache the last 25 messages on the device so repeat opens read nothing. The estimate guides the design before a single real user signs in.

What are the common mistakes?

The most common mistakes in a Flutter and Firebase chat app are weak security rules, ordering by device time, and loading every message at once.

  • Open security rules. Rules that let any signed-in user read any chat leak private messages. Test the rules with a second account before launch.
  • Device timestamps. Ordering by the phone's clock scrambles messages when clocks disagree. Always use a server timestamp.
  • No paging. Loading a whole chat history at once is slow and burns reads. Load 25 to 50 messages and page older ones on scroll.
  • Notifications to everyone. Alerting the sender and users already in the chat annoys people. Skip both in the Cloud Function.
  • Full-size uploads. Sending camera photos at full size is slow on mobile networks. Resize before upload.

Key takeaways

  • Flutter builds the screens while Firebase handles accounts, messages, files and alerts
  • Model chats as rooms with a messages subcollection, and keep each message document small and flat
  • Listen with a stream for live updates, page messages in batches, and order by server timestamp
  • Send push alerts from a Cloud Function that skips the sender and users already in the chat
  • Write the Firestore security rules before the first test user signs in

Frequently asked questions

Do I need my own server for a Flutter chat app with Firebase?

No. Firebase provides the hosted services a chat app needs: Auth for sign-in, Firestore for live messages, Storage for media, Messaging for push alerts and Functions for server logic. A small team can ship a working chat app without running or maintaining a server of its own.

Is Firebase free for a small chat app?

Firebase has a free tier that covers small usage, and charges apply as usage grows. The exact limits and prices change, so check the Firebase pricing page for the current figures rather than relying on a quoted number. The worked example in this article shows how to estimate reads and storage before launch.

Can I use Firebase chat in a Flutter web app too?

Yes. The FlutterFire plugins work on web as well as Android and iOS, so the same codebase can run the chat in a browser. Push notifications on the web need extra setup, and some features behave differently, so test the web build separately rather than assuming the mobile build covers it.

How do I keep chat messages private in Firestore?

Write security rules that only let chat members read a room's messages, and store the member list on the chat document so the rules can check it. Test the rules with a second account before launch, and never rely on hiding a chat id as the only protection.

What is the difference between Firestore and Realtime Database?

Both are Firebase databases, but Firestore is the newer document database with richer queries, better offline support and paging, which fits chat apps well. Realtime Database is a simpler JSON tree that can be cheaper for very high-frequency small updates. Most new chat apps choose Firestore.

How do I add video or voice calls to a Flutter chat app?

Firebase does not provide calling on its own, so teams usually add a calling service or SDK alongside Firebase for the media stream, while keeping Firebase for accounts, chat and signalling. Plan the call flow separately from the chat flow, because calls need connection handling, permissions and a different user interface.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: software products in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building a chat app in Flutter? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Mobile App Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to contact@hertiv.com.

Prefer email? Write to contact@hertiv.com
Estimate costBook a call