Flutter + Firebase

Santhosh Adiga U
4 min readApr 1, 2023

--

Firebase Firestore is a NoSQL document-oriented database that provides real-time data synchronization and offline data persistence to mobile and web applications. It is a cloud-based database that stores data in a flexible, hierarchical format, allowing developers to store, retrieve, and query data with ease. Firebase Analytics, on the other hand, is a tool that helps developers track user behavior, measure app performance, and gain insights into their audience.

In this article, we’ll explore how to integrate Firebase Firestore Database and Analytics in a Flutter application. We’ll cover the following topics:

1. Setting up a Firebase project

2. Adding Firebase dependencies to Flutter app

3. Creating a Firestore collection

4.Adding data to Firestore

5. Reading data from Firestore

6. Integrating Firebase Analytics in Flutter

Setting up a Firebase project

To get started with Firebase Firestore and Analytics, you’ll need to create a new Firebase project. Go to the Firebase console, sign in with your Google account, and click “Add project.” Follow the instructions to set up your project.

Adding Firebase dependencies to Flutter app

Once you have your Firebase project set up, you’ll need to add the Firebase dependencies to your Flutter app. To do this, add the following dependencies to your pubspec.yaml file:

dependencies:
firebase_core: ^1.6.0
cloud_firestore: ^3.1.5
firebase_analytics: ^8.3.3

Then run the following command in your terminal to install the dependencies:

flutter pub get

Creating a Firestore collection

To create a Firestore collection, first, you’ll need to initialize Firebase in your Flutter app. Add the following code to your main.dart file:

import 'package:firebase_core/firebase_core.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}

Next, create a Firestore collection by adding the following code:

import 'package:cloud_firestore/cloud_firestore.dart';

FirebaseFirestore firestore = FirebaseFirestore.instance;
CollectionReference users = firestore.collection('users');

This code creates a users collection in Firestore.

Adding data to Firestore

To add data to Firestore, you can use the add() method on a CollectionReference. For example:

await users.add({
'name': 'John Doe',
'email': 'johndoe@example.com',
});

This code adds a new document to the users collection with a name and email field.

Reading data from Firestore

To read data from Firestore, you can use the get() method on a CollectionReference or a DocumentReference. For example, to get all the documents in the users collection, you can use the following code:

QuerySnapshot snapshot = await users.get();
snapshot.docs.forEach((doc) {
print(doc.data());
});

This code retrieves all the documents in the users collection and prints the data for each document.

Integrating Firebase Analytics in Flutter

To integrate Firebase Analytics in your Flutter app, you’ll need to initialize Firebase Analytics and log events. To initialize Firebase Analytics, add the following code to your main.dart file:

import 'package:firebase_analytics/firebase_analytics.dart';

FirebaseAnalytics analytics = FirebaseAnalytics();

To log an event, use the following code:

await analytics.logEvent(
name: 'button_click',
parameters: {
'screen': 'home',
'button': 'login',
},
);

This code logs a button_click event with 6. Integrating Firebase Analytics in Flutter…

with additional parameters screen and button.

Firebase Analytics can help you track user behavior, measure app performance, and gain insights into your audience. You can use it to track events such as app launches, screen views, button clicks, and more.

In addition to logging events, Firebase Analytics also provides a dashboard where you can view your app’s analytics data. To access the dashboard, go to the Firebase console and select your project. Then click “Analytics” from the menu on the left.

Here’s a complete example of a Flutter app that integrates Firebase Firestore, Database and Analytics:

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_analytics/firebase_analytics.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}

class MyApp extends StatelessWidget {
final FirebaseFirestore firestore = FirebaseFirestore.instance;
final CollectionReference users = FirebaseFirestore.instance.collection('users');
final FirebaseAnalytics analytics = FirebaseAnalytics();

Future<void> _addUser() async {
await users.add({
'name': 'John Doe',
'email': 'johndoe@example.com',
});

await analytics.logEvent(
name: 'user_added',
parameters: {
'name': 'John Doe',
'email': 'johndoe@example.com',
},
);
}

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firebase Demo',
home: Scaffold(
appBar: AppBar(
title: Text('Firebase Demo'),
),
body: Center(
child: ElevatedButton(
onPressed: _addUser,
child: Text('Add User'),
),
),
),
);
}
}

In this example, we have a MyApp class that initializes Firebase Firestore, creates a users collection, and initializes Firebase Analytics. The addUser method adds a new user to the users collection and logs a user_added event with Firebase Analytics.

The build method creates a simple Flutter app with an ElevatedButton that calls the addUser method when pressed.

Conclusion

Firebase Firestore and Analytics are powerful tools for building robust and data-driven Flutter applications. With Firestore, you can store and retrieve data in real-time, while with Analytics, you can track user behavior and gain insights into your audience. By integrating these tools in your Flutter app, you can build a more engaging and successful mobile application.

In this article, we’ve covered how to set up a Firebase project, add Firebase dependencies to a Flutter app, create a Firestore collection, add data to Firestore, read data from Firestore, and integrate Firebase Analytics in Flutter. With these skills, you’re well on your way to building a world-class Flutter application with Firebase.

--

--

Santhosh Adiga U
Santhosh Adiga U

Written by Santhosh Adiga U

Founder of Anakramy ., dedicated to creating innovative AI-driven cybersecurity solutions.

No responses yet