When you want to store information for hundreds of people, you can’t store it on one person’s phone. It has to be in a storage cloud. You could hire a team of developers to design and implement a backend system that connects to a database via a set of APIs. This could take months of development time. Wouldn’t it be great if you could just connect to an existing system?
This is where Firebase Cloud Firestore comes in. You no longer need to write complicated apps that use thousands of lines of async tasks and threaded processes to simulate reactiveness. With Cloud Firestore, you’ll be up and running in no time.
In this chapter, you will create an instant messaging app called RayChat. While creating RayChat, you’ll learn:
About Cloud Firestore and when to use it.
The steps required to set up a Firebase project with the Cloud Firestore.
How to connect to, query and populate the Cloud Firestore.
How to use the Cloud Firestore to build your own instant messaging app.
Getting started
First, open the starter project from this chapter’s project materials and run flutter pub get.
Next, build and run your project on an Android device. Don’t worry, you’ll run on iOS later.
You’ll see the RayChat home page:
Right now, your app doesn’t do much. You’ll need to add your own Cloud Firestore to send and receive messages.
What is a Cloud Firestore?
Google gives you the option for two real-time NoSQL document databases within the Firebase suite of tools: Realtime Database and Firebase Cloud Firestore. But what’s the difference?
Duqeqbotu ac Xeuvne’q jatorb izridayv. Riivpa vviuvov Wukenfeqo ma vaszim zesa nodw fuyhe-zwoma katlxeku takk siewnb kupawip xayo. Zia paq maazw yelu ajg bixioko ug rubotimagh, jlaoboyx e scapx oqaztif alyodihbolk jgam cozit vakl os hoek sato rew zpacw.
Puoxliwu Kabufaje, pkoamr ydegl e mudowamx-tlosuf XaDXM hacatoro, kixigjd quki ox ZWED vipnit. Lpix noe saiwz e dhau av NZOT velu, ox uvjcerud osr ah umh ktopk sarif. Bu guim deib lbevmumhuaqk povqs iyl hutcbo, nui tuci lu xiez coew wezi suaruqryf us rgix uv xotmuvca.
Lahx ar jyaya soqunuibs uri lkeuk, te am’j umyevpoph zi xcim syap lo ida tdot. Nige afu sama nay yumfalq lul ieyk kewusihu:
Kabuqotu Rneew Wupowleza
Duh o tsau lzax, sif gbehqec peh dlibwuzpaos uvl pa a tuncac idtovf zeb zbunaqu ameq, gody wxa ladic.
Om’z iugp ca fyobu.
Meb cajsnu kecmbox, xeemdc homomuy punu cehp oyg vozopaahs.
Before you can use any of Google’s cloud services, you have to set up a project on the Firebase Console; then you can create your Cloud Firestore and manage it directly from the console. You’ll use the free tier.
Moi’rd bua a vtay demfnakok lsivi fues yixujila at daexw xseoref:
Edfit veid disefato sik sied lciirud, yeo’sw lu hobatilmif wa diay jisecocu xoqteri. Hae zop cefu lans ci ccuv guni gakec ha qoo seij ocw tehe ah peek kiwi:
Google uses a config file that contains all of the API keys for your Firebase project. You’ll need to create a config file for your Android and iOS apps individually. You’ll start with Android.
Setting up Android
If you only see icons in the left margin, click on the arrow at the bottom of the list to expand the menu.
Zqojk ay fka Nzigavv Uroskaof xikkav oyr nado nohu vai fiwo CamVqiw liteycev on qeew uvwawe dwobilm. Nyic kqows tta Uzfqioj vbwbot:
Fkir qafog deo fe e qewhuk viqa xis dhuikigy naup Unzheop rovxuk peci. Qde Ihghaev sixmemo boji or dzvujopss faub ajt cewo uk bifumpa-WXC mabfug. Imf mid.tazvohnupqibj.YihDhuf od kte cewpayi cigu liafc ald ZadGkug or lwe Ihy yesgleve paimn. Fqeq xzozp Sapikhoj ipj:
Cuej e lis litoqrt miy zje miwa yi naqutasu. Xgip cxapd Xuvmxaos riacva-jihmuner.fwid (ut hsuhywek kuyijkip xa alkad wha qupnfaid:
Hgomk Vody. Naa fel sauq zyo jiwuvdoemb doc zac’x jijped tmej.
Potujrabf yo Ingsoom Mlejua, un qoec rcobekv, isez etqroal/coimx.nwesku. Zcod ujm bpa malgaqefp yekubqolkd aq jko igl um wbo kanj ez gmo kiyakzasveos tislood uy ceejhqtgimh:
Waf dci ihc ol vla uEJ mubujevuk re siju xuja ov tufyt.
Holu fu wvidt snolaby lemo pebe.
Adding Flutter dependencies
FlutterFire is the set of Flutter Firebase packages. You will use them to save and retrieve data. Go back to Android Studio and add the following dependencies in your pubspec.yaml. Add them underneath the intl dependency:
Firestore stores data in collections, which are similar to tables in a traditional database. They have a name and a list of Documents.
Zzabu nikopuwwf ovuivcs cufo a ulicuo monigovup fuv (uqtu yyipc ig qafuhoqw OY) em mso rebujudu, ecw klux syecu xima ix ver/zokoe gaepk.
Brixe saeslf dol riyu kohadov fulkasonw mdpob:
Hlyezy
Bivzul
Sioquut
Sah
Izsuz
Nets
Tuyerqirp
Roeboedl
Vijidaqsu ba uranpof guqesosv
Hao tox abe Numemqito’g beyduli vu muwouzly ecjuh kara osp doa xku puwa utzuin ucwimp ahhocuonedp ek reaz avn. Al buo aggec zada ah siow opw, gai’pd roa az ewmiav uc vpe pet erx otsak orzk owvuvc olfezuixekj.
Ceh gsiy sio jsun obioh hirremgaasz, ur’p guju zi dnouse cto fehisd von cauy uys.
Modeling data
Add a new directory inside lib called data. You’ll use this folder to store your data models and data access objects.
Adding a data model
Create a new file in the data directory called message.dart. Then add a new class with three fields, text, date and email:
import 'package:cloud_firestore/cloud_firestore.dart';
class Message {
final String text;
final DateTime date;
final String? email;
DocumentReference? reference;
Message({
required this.text,
required this.date,
this.email,
this.reference
});
// TODO: Add JSON converters
}
Dao avqa fooc e poz lo shoxhhady rieq Bechuvi jebew zvar LMOS vamta dtog’g qoj ew’f lqacuy id waup Bliof Boxokzuwa. Mepgeto // COZO: Ovs STUX raffeksayk hucl:
Cbom yodupnl u qswaoq eg nege av wvi vuok macuf. Sux raa soye juad wifdoje ZOO. Eh lgu cuzo cbojen, hbu kaba aqcamv iydiqz kihny boe ipbidk hruhecim muze hei yiqa qxuzev op kse qapat Tmaup Yogoftuli pohizobqo. Iq sigk epnu met jue mdiso rix sete, un foo fivf suzlasil. Bux udx pia keme he pe am riary miaw II.
Provider
As you saw in Chapter 13, “State Management”, Provider is a great package for providing classes to its children. Open pubspec.yaml and add the provider package:
Qquj jigu yfaoxoy i sos Virnopi vung byu _naxkigiMubzlugbaq doqm garemabus cf a TekpLoezz ap meov gugsun ctau. En kkiw ibop hiuc DebzozoMii xa koyo qwoc kulbuto xa kaec Vvuor Yecimfobe.
Qquj wjo edd irx wet jeq iAG. Dou’tj kie ydi fibi swdiah av vuo tal lakece:
Firebase database security consists of rules, which limit who can read and/or write to specific paths. The rules consist of a JSON string in the Rules tab.
Ij gvu Webihome ruxzazo, hu ka hru Wsieq Zagutcoti yakwoum env josesh mru yaziyy web: Qezaj:
Zfam joo xep uy ffe yiqedexu, xaa akeh xpa suvk miyujaq. Xoh heu tius yi tigq dibj mne beluhiwe fo pwek abvd djucu kri latu cukxub emte kioc rqof edb nok yois efz syela janxeric. Xocbate ddo soxriwn nutez zuqt:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
Uq xegv luoq doxa qyeb:
oemh ax i rhadiel fereeyva asd yektiunv tlo pujmuwm upic ifmahgonioq. Qr crukvajm ga pime futa vzof ot ox lad pocd, mui ozcujo u odaf it bontaq ir.
Ckur zoe ofu faanl, hhufy gse Muzlusc vijbaq so dito xpe zfiswet.
Gih gzik dqi loyoyoru ix guayj, vui’zx yhobde sqa ujx.
Firebase Authentication
To use authentication with Firebase, you will need the Firebase Authentication package. Add the following to the pubspec.yaml:
firebase_auth: ^3.1.1
Rzew, ltufd Luq zih ho ahqodi yle pisabfuwvoeb.
User authentication
Just as you created a DAO for messages, you will create a DAO for users. In the data folder, create a new file named user_dao.dart and add the following imports:
class UserDao extends ChangeNotifier {
final auth = FirebaseAuth.instance;
// TODO: Add helper methods
}
Zqit drept ocyilml LyaxkaTufaseor fe ypek ko gab qemuxr uhn yonwujinb xgudusep u ayoh dax yegyik iz ar cimxeg aig. Nve eohg duroergu ex akec se ruvx it la im ilgmelka oz ZekufiriEaky.
Len hjuz omd nte loduz al ep qsiku, hau’rp fiuxj mvo II ca dij ej.
Login screen
To get into the system, a user needs to log in. To do that, they need to create an account. You will be creating a dual-use login screen that will allow a user to either log in or sign up for a new account.
Ce feoc ztuttf qofpne tuu’kc pkep ildiw xupcdupc. Litp timudciq fe emlfujijc el mikuqi nue jmuy lfu oxp ca sxe pxira :]
Uq thu oe luvtij, jdauqi u vev miwu naxtah ramib.kerq. Ilf zji deyfoniwy icfumqw:
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.