In today’s fast-paced digital world, user authentication and database management are critical for mobile applications. Firebase Authentication Flutter provides a seamless way to integrate authentication services, while Firebase Firestore with Flutter and Firebase Realtime Database Flutter enable efficient data storage and retrieval. This guide will walk you through Flutter Firebase Integration, from setting up Flutter User Authentication to managing data securely.
google-services.json
file.Add the required dependencies to your pubspec.yaml
:
dependencies:
firebase_core: latest_version
firebase_auth: latest_version
cloud_firestore: latest_version
firebase_database: latest_version
Run flutter pub get
to install dependencies.
Modify main.dart
to initialize Firebase:
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
Firebase Authentication Flutter allows user authentication via email, Google, Facebook, and more. Here’s how to implement email authentication:
Sign Up with Email & Password
import 'package:firebase_auth/firebase_auth.dart';
Future<User?> signUpWithEmail(String email, String password) async {
try {
UserCredential userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email,
password: password,
);
return userCredential.user;
} catch (e) {
print("Error: $e");
return null;
}
}
Sign In with Email & Password
Future<User?> signInWithEmail(String email, String password) async {
try {
UserCredential userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: password,
);
return userCredential.user;
} catch (e) {
print("Error: $e");
return null;
}
}
Implementing Google Authentication
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';
Future<User?> signInWithGoogle() async {
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
if (googleUser == null) return null;
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
UserCredential userCredential = await FirebaseAuth.instance.signInWithCredential(credential);
return userCredential.user;
}
Firestore is a NoSQL cloud database that allows real-time syncing. Here’s how to add and retrieve data:
Add Data to Firestore
import 'package:cloud_firestore/cloud_firestore.dart';
Future<void> addUser(String uid, String name, String email) async {
await FirebaseFirestore.instance.collection('users').doc(uid).set({
'name': name,
'email': email,
});
}
Retrieve Data from Firestore
Stream<DocumentSnapshot> getUser(String uid) {
return FirebaseFirestore.instance.collection('users').doc(uid).snapshots();
}
Realtime Database is another Firebase database that syncs data instantly across devices.
Write Data
import 'package:firebase_database/firebase_database.dart';
final DatabaseReference dbRef = FirebaseDatabase.instance.reference();
dbRef.child("users").child(uid).set({
"name": "John Doe",
"email": "john.doe@example.com"
});
Read Data
dbRef.child("users").child(uid).once().then((DataSnapshot snapshot) {
print(snapshot.value);
});
Firebase allows easy user management, such as password reset and email verification.
Reset Password
Future<void> resetPassword(String email) async {
await FirebaseAuth.instance.sendPasswordResetEmail(email: email);
}
Sign Out
Future<void> signOut() async {
await FirebaseAuth.instance.signOut();
}
By integrating Firebase Authentication Flutter with Flutter Firebase Integration, you can build secure and scalable applications with real-time data synchronization. This guide covered setting up Flutter User Authentication, using Firebase Firestore with Flutter, and managing data with Firebase Realtime Database Flutter. Follow this Flutter Firebase Setup Guide to create powerful apps with secure authentication in Flutter and efficient user management.