0% found this document useful (0 votes)
18 views33 pages

Firebase

Uploaded by

aalifnier
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views33 pages

Firebase

Uploaded by

aalifnier
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Flutter -Database

firebase
‫ سندس عكي‬. ‫أ‬
Flutter Database Concepts

A database is an organized collection of data, which supports the storage and


manipulation of data and accessed electronically from a computer system. We
can organize data into rows, columns, tables, and indexes. It makes data
management easy. We can store many things in the database, like name, age,
picture, image, file, pdf, etc.
Flutter provides many packages to work with the database. The most used and
popular packages are:
sqflite database: It allows to access and manipulate SQLite database.
Firebase database: It will enable you to access and manipulate the cloud
database.
difference between sqlite , firebase
SQLite Database
Overview:
- SQLite is a lightweight, serverless, self-contained SQL database engine that is embedded within
the Android application.
- It allows developers to store, retrieve, and manipulate structured data locally on the device.
Key Features:
- Local Storage: Data is stored locally on the device, making it accessible even without an
internet connection.
- SQL Support: Uses standard SQL syntax for queries, which is familiar to many developers.
- Performance: Fast for local data operations, especially with small to medium datasets.
- Data Model: Supports complex queries, joins, and transactions, making it suitable for relational
data.
Use Cases:
- Ideal for apps that require local data storage, such as note-taking apps, offline games, or any
application that needs to function without an internet connection.
difference between sqlite , firebase
Firebase
Overview:
- Firebase is a Backend-as-a-Service (BaaS) platform that provides various cloud services, including a NoSQL
database called Firestore and the Realtime Database.
- It is designed for developing web and mobile applications quickly and efficiently.
Key Features:
- Cloud Storage: Data is stored in the cloud, allowing for real-time data synchronization across multiple
devices.
- NoSQL Database: Uses a document-based structure (Firestore) or a JSON tree structure (Realtime Database),
which can be more flexible than traditional SQL.
- Real-time Updates: Changes to the database can be reflected in real-time across all connected clients.
- Scalability: Easily scales to handle large amounts of data and user load without significant changes to the
infrastructure.
- Integrated Services: Provides various services such as authentication, analytics, and cloud functions, which
can be tightly integrated with the database.
Use Cases:
- Best suited for applications that require real-time data updates, user authentication, and cross-device
synchronization, such as chat apps, collaborative tools, and social media platforms.
difference between sqlite , firebase
Summary
SQLite is best for local data storage with complex queries and relationships,
while Firebase is ideal for applications that need real-time data synchronization
and cloud-based services.
Choose SQLite for offline-first applications or when you need a structured
relational database, and opt for Firebase when you need to handle real-time
updates, scalability, and integrated backend services.
firebase
provides a comprehensive suite of tools and services to help
developers build, improve, and grow their mobile and web
applications. It offers a variety of features, including real-time
databases, cloud storage, authentication, hosting, and
analytics, making it easier for developers to manage their app's
backend and enhance user engagement. Firebase is widely
used for building applications quickly without needing to
manage infrastructure, allowing developers to focus on
creating great user experiences.
Here are the main types and services offered
by Firebase:
Firebase Realtime Database: A NoSQL database that allows for storing and
syncing data in real-time across clients and devices.
Cloud Firestore: A flexible and scalable NoSQL database for storing and
retrieving data, supporting complex queries.
Firebase Authentication: A service that simplifies user sign-in with various
methods, including email/password and social media accounts.
Firebase Cloud Storage: A service for storing files, such as images and videos,
with secure and easy access.
Firebase Hosting: A web hosting service that enables quick deployment of web
applications with content distributed over a Content Delivery Network (CDN).
Cloud Functions: A serverless environment that lets you run custom code in
response to events within Firebase, such as database updates or HTTP requests.
Firebase Analytics: A tool for analyzing user data and interaction with your app,
helping developers understand user behavior to enhance their experience.
Firebase Cloud Messaging (FCM): A service for sending notifications and
messages to users across different devices.
Firebase Remote Config: Allows you to modify the behavior and appearance of
your app remotely without needing to publish an update.
Firebase Test Lab: A testing environment for Android and iOS apps, allowing you
to test on a variety of real and virtual devices.
1
Firebase Authentication

Firebase setup
1 - install firebase_core https://pub.dev/packages/firebase_core/install

2- add a line like this to your package's pubspec.yaml under dependencies


';
firebase_core: ^3.8.1
3- Now in your main.dart add:

import 'package:firebase_core/firebase_core.dart
2

install firebase_auth
1- visit this site => https://pub.dev/packages/firebase_auth/install

2- add a line like this to your package's pubspec.yaml under dependencies


firebase_auth: ^5.3.4
3- Now in your main.dart add:

import 'package:firebase_auth/firebase_auth.dart';
3

Download node.js
https://nodejs.org/en
4

firebase.google.com
https://console.firebase.google.com/u/0/

1- Add account in firebase


2- add project
5

Download Firebase CLI


1- In firebase app choose flutter as shown below
5

Download Firebase CLI


2- install firebase cli

3- run firebase cli & login with firebase account


5

Download Firebase CLI


4- copy => npm install -g firebase-tools

5-past it on command- prompet in vscode


5

Download Firebase CLI


6- run firebase login on command- prompet in vscode

7-back to console firebase =>Next


Download Firebase CLI
8- Copy this line and past it on command- prompet in vscode
9- Add the path in environment variable

Restart vscode
10- Copy this line and past it on command- prompet in vscode

Will download firebase_option.dart


11-back to console firebase =>Next
12- In main.dart

Future<void> main() async {


WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const MyApp());
}
Sign in and sign up using authentication
1- go to https://console.firebase.google.com/ and
choose your project then press android button
2- in flutter project go to android >>app>>build.gradle
defaultConfig {
applicationId "com.example.flower_app“
Copy com.example.flower_app and past it on firebase website

Then press Register app


3- Download google-services.json file

Put it under android>>app


4- open android>>build.gradle
Make sure that google() exist inside repositories
allprojects {
repositories {
google()
mavenCentral()
}
}
dependencies {
// START: FlutterFire Configuration
classpath 'com.google.gms:google-services:4.3.15'
// END: FlutterFire Configuration
classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
5 – android >> app>>build.gradle
apply plugin: 'com.android.application'
// START: FlutterFire Configuration
apply plugin: 'com.google.gms.google-services'
// END: FlutterFire Configuration
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
In register page
1- import 'package:firebase_auth/firebase_auth.dart';
2- save value of user input(email,password) under register wedgite
late String email;
late String password;
3- TextField(
keyboardType: TextInputType.emailAddress,
obscureText: false,
onChanged: (value){
email=value;
}
In register page
4- TextField(
keyboardType: TextInputType.text,
obscureText: true,
onChanged: (value){
password=value;
},
In register page

5-Add new authentication instance


final _auth=FirebaseAuth.instance;
late String email;
late String password;
In register page
6-
levatedButton(
onPressed: () async{
try {
final newuser= await _auth.createUserWithEmailAndPassword(email: email,
password: password);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => Home()),
);
} catch (e) {
print(e);

}
},
In sign in page
1- import 'package:firebase_auth/firebase_auth.dart';

2-final _auth=FirebaseAuth.instance;
late String email;
late String password;

3- onChanged: (value){ 4- onChanged: (value){


email=value; password=value;
},
},
5- onPressed: () async {
try {
final user= await _auth.signInWithEmailAndPassword(email: email,
password: password);
if(user != null)
{ Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => Home()),
);
}
} catch (e) {
print(e);
}

You might also like