YouTip LogoYouTip

Flutter Firebase

Firebase Integration | Rookie Tutorial Firebase is a mobile and web application development platform provided by Google. This section will introduce how to integrate Firebase in Flutter. --- ## Firebase Overview Firebase provides various backend services, including authentication, database, storage, analytics, etc. | Service | Description | | --- | --- | | Firebase Auth | User authentication | | Cloud Firestore | Cloud NoSQL database | | Realtime Database | Real-time database | | Cloud Storage | Cloud file storage | | Firebase Analytics | Application data analytics | | Cloud Functions | Cloud serverless functions | --- ## Installation and Configuration ### Add Dependencies dependencies: firebase_core: ^2.0.0 firebase_auth: ^4.0.0 cloud_firestore: ^4.0.0 ### Firebase Initialization ## Example: Initialize Firebase import'package:firebase_core/firebase_core.dart'; import'package:flutter/material.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); // Initialize Firebase await Firebase.initializeApp(); runApp(const MyApp()); } --- ## Firebase Auth - User Authentication ## Example: Email and Password Login import'package:firebase_auth/firebase_auth.dart'; class AuthService { final FirebaseAuth _auth = FirebaseAuth.instance; // Register Future registerWithEmail(String email, String password) async { try{ final credential = await _auth.createUserWithEmailAndPassword( email: email, password: password, ); return credential.user; } on FirebaseAuthException catch(e){ print('Registration failed: ${e.message}'); return null; } } // Login Future signInWithEmail(String email, String password) async { try{ final credential = await _auth.signInWithEmailAndPassword( email: email, password: password, ); return credential.user; } on FirebaseAuthException catch(e){ print('Login failed: ${e.message}'); return null; } } // Sign out Future signOut() async { await _auth.signOut(); } // Listen to login status Streamget authStateChanges { return _auth.authStateChanges(); } } --- ## Cloud Firestore - Database ## Example: Firestore Basic Operations import'package:cloud_firestore/cloud_firestore.dart'; class FirestoreService { final FirebaseFirestore _db = FirebaseFirestore.instance; // Add document Future addUser(Map user) async { await _db.collection('users').add(user); // Or specify ID // await _db.collection('users').doc('user_id').set(user); } // Get single document Future?> getUser(String id) async { final doc = await _db.collection('users').doc(id).get(); return doc.data(); } // Get all documents in collection Future>> getUsers() async { final snapshot = await _db.collection('users').get(); return snapshot.docs.map((doc)=> doc.data()).toList(); } // Conditional query Future>> getUsersByAge(int age) async { final snapshot = await _db .collection('users') .where('age', isGreaterThanOrEqualTo: age) .get(); return snapshot.docs.map((doc)=> doc.data()).toList(); } // Update document Future updateUser(String id, Map data) async { await _db.collection('users').doc(id).update(data); } // Delete document Future deleteUser(String id) async { await _db.collection('users').doc(id).delete(); } // Real-time listener Stream watchUsers(){ return _db.collection('users').snapshots(); } } --- ## Firebase Storage - File Storage ## Example: File Upload import'package:firebase_storage/firebase_storage.dart'; import'dart:io'; class StorageService { final FirebaseStorage _storage = FirebaseStorage.instance; // Upload file Future uploadFile(File file, String path) async { try{ final ref = _storage.ref().child(path); final result = await ref.putFile(file); return await result.ref.getDownloadURL(); } on FirebaseException catch(e){ print('Upload failed: ${e.message}'); return null; } } // Get download URL Future getDownloadURL(String path) async { try{ final ref = _storage.ref().child(path); return await ref.getDownloadURL(); } on FirebaseException catch(e){ print('Failed to get URL: ${e.message}'); return null; } } // Delete file Future deleteFile(String path) async { await _storage.ref().child(path).delete(); } } > Firebase provides powerful backend services and is the best choice for quickly building backends for Flutter applications.
← Codex ConceptsFlutter Build Release β†’