KEMBAR78
Intro to Firebase || Event by DSC UNIDEB | PDF
Dominik Šimoník
Co-founder @yadada / Firebase GDE / GDG Ostrava
@ryzizub
Firebase
Introduction
With example :)
Firebase
Helps app teams succeed
Backend-as-a-Service
Service provided by Google
Authentication
Prebuild UI’s
Supports Auth2
Google, Facebook, Github...
Firestore
NoSQL Cloud Database
Modelled as collection of
documents
Horizontally scaled
Firestore vs Realtime Database
● JSON Tree
● Just basic queries
● Limited by region
● Limited scaling
● Collections of documents (JSON)
● Strong queries (sort and query)
● Regional and multi-regional
● Automatic scaling
What is Cloud Firestore?
● Newer solution inspired by Realtime database
● Modeled as collections of documents
● Stronger queries
● Pay for reads/writes
● Horizontally scaled
● Shallow by default
Cloud Storage
Object storage service
High scalability
High availability
Any file
Firebase Hosting
Free Hosting
Can serve from your CSS and HTML files to
your Express.js microservices or APIs.
Backups
Cloud Functions
Run your backend code without
managing servers
Triggers
● Cloud Firestore Triggers
● Realtime Database Triggers
● Remote Config Triggers
● Firebase Authentication Triggers
● Google Analytics for Firebase
Triggers
● Crashlytics Triggers
● Cloud Storage Triggers
● Cloud Pub/Sub Triggers
● HTTP Triggers
Machine Learning
Prebuild API’s
AutoML
Custom Tensorflow models
Analyze
Google analytics
App measurement solution
App usage
App engagement
Cloud Firestore
Offline support
● Při získání jakýkoliv dat, vše se automaticky ukládá do
lokální cache
Performance
monitoring
Easy tracking of app’s
performance issues
Firebase
Crashlytics
Realtime crash reporter
Cloud Firestore
Offline support
● Při získání jakýkoliv dat, vše se automaticky ukládá do
lokální cache
SocialApp
Sign in / up
● Facebook login
● Twitter login
● Google login
● Email login
● Registration
● Close
Activity
● Feed
● Images
● (Notification)
Profile
● User information
● Profile image
Nearby places
● Images
● Comments
● (Search)
Firebase use
Sign in / up
Firebase Authentication
● Facebook login
● Twitter login
● Google login
● Email login
● Registration
● Close
e
Activity
Realtime Database / Cloud Firestore
Cloud Functions
Cloud Storage
Cloud Messaging
● Feed
● Images
● (Notification)
Profile
Cloud Firestore
Cloud Storage
Firebase Authentication
● User information
● Profile image
Nearby places
Cloud Storage
Cloud Firestore
● Images
● Comments
● (Search)
firebase_core: "0.7.0"
Core
Sign in
firebase_auth: "^0.20.0+1"
google_sign_in: ^4.5.1
Google Sign in
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final GoogleSignInAccount gUser = await GoogleSignIn().signIn();
final GoogleSignInAuthentication gAuth = await gUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: gAuth.accessToken, idToken: gAuth.idToken);
var result = await _firebaseAuth.signInWithCredential(credential);
Google sign in
final authResult = await FirebaseAuth.instance.signInAnonymously();
Anonymous sign in
Activity
storage: ^7.0.0
cloud_firestore: "^0.16.0"
Google Sign in
final FirebaseStorage _fsStorage = FirebaseStorage.instance;
TaskSnapshot task = await _fsStorage.ref(path).putFile(file,
metadata);
var url = task.ref.getDownloadURL();
File upload
final FirebaseFirestore _fsSt = FirebaseFirestore.instance;
_fsSt.collection('feed').add({
'title': 'Hello Flutter OnAir 2021',
'content': 'How are you?',
});
Database
CollectionReference urRef = _fsSt.collection('feed');
return StreamBuilder<QuerySnapshot>(
stream: users.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot)
{
return new ListView(
children: snapshot.data.docs.map((DocumentSnapshot document) {
return new ListTile(
title: new Text(document.data()['title']),
subtitle: new Text(document.data()['content']),
);
}).toList(),
);
Feed List
firebase_messaging: "^8.0.0-dev.11"
Notification
Background
Firebase
Foreground
Notification
FirebaseMessaging.onBackgroundMessage(backgroundHandler);
static Future<void> backgroundHandler(RemoteMessage message) async {
NotificationData latest = NotificationData.fromMap({
'title': message.data['title'],
'body': message.data['body'],
});
await LocalDatabase().saveLatestNotification(latest);
}
Code slides use Source Code Pro
Background
FirebaseMessaging.onMessage.listen((message) {
var latest = NotificationData.fromMap({
'title': message.data['title'],
'body': message.data['body'],
});
});
Code slides use Source Code Pro
Foreground
That’s it
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;
Isolate.current.addErrorListener(RawReceivePort((pair) async {
final List<dynamic> errorAndStacktrace = pair;
await FirebaseCrashlytics.instance.recordError(
errorAndStacktrace.first,
errorAndStacktrace.last,
);
}).sendPort);
Code slides use Source Code Pro
Background
firebase_performance: "^0.5.0+1"
Performance monitoring
Security
What are Security rules?
● Provide access control and data validation
● Flexible rules syntax allows you to create rules that match anything
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /burgers/{burgerId}/reviews/{reviewId} {
allow write: if request.resource.data.stars > 0;
allow read: if resource.data.visibility == 'public';
}
}
}
rules_version = '2';
function isValidItemSchema() {
return hasFields(
incomingData(),
['bought', 'internalNumber', 'invoiceNumber', 'location', 'name', 'notes',
'serialNumber', 'status', 'type', 'user', 'warrantyUntil', 'createdBy', 'createdAt',
'costCenter'],
['updatedBy', 'updatedAt', 'parent', 'children']
)
&& (isMissingField(incomingData(), 'children') || incomingData().children is map)
&& incomingData().createdBy is string
&& incomingData().createdAt is timestamp;
}
Thank you!
@ryzizub

Intro to Firebase || Event by DSC UNIDEB