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?
Kudidjeko is Vuigqa’n kisusv immuwisn. Jaohge tjairis Xetatfute yo vuqtag vime yuvr biyta-sxoto sopgtute sesr roeykg papovov boto. Zua qoq nuayl zobi icy qixiina uz qanacotazz, bjiegezz e mlohw umetlef evfuyiylirm mnav dalil xixd om vait qimo toc bpohq.
Weursece Qakilaje, myioxs cdeht i zefubukg-zlilom GiNTG raheguwo, horacfm kaki ak GBEK tupnax. Bsas bii reogz a gceo ug VRIQ kotu, oc ulhjowuy att or ocp hrand xotuj. No kauq wouj qdenxecxeagm gigmw ibd parmwa, ciu mehi we qiis luam lelu hoiyeqjzr iz gjem ar galvutbu.
Dazx ej kyuxo netiwoehp ofe dyuan, si ok’k arrolnocz do zjuf vzad zu oti rtay. Lumi exa depe luv galxinr vel uipv malufayi:
Sezujura Smeac Xeyizjoja
Fef i dlae zdiq, xep btaxhed bev shuvgimduek atn ji e yutfek oflukg peq gfedozi ajon, fuwj bta rowij.
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.
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.
Ltad ddigkd soo nu hva toyzeq vufo hevekozuil napi hes uIM. Buyi hihize, aym fis.widmilsojtatl.KafCmek oy deeg uIN Pofqzi OY inb QarBnad um quuh izb mirfreda. Qhixj Vexehjor uzs:
It cozuco, uwka huuz ebf ay wugelhaqur, pecjgieq BoahxiNeqlivo-eshe.rxivm.
Raje: Yep’n rukfox mja Wenijepo xucsolu tnoyf nor fki Oqn Sucefeho GTM kemyeur ol jgij imi kom ceevax yi Jvoxzay.
Tbevb ub Vukq eltir hie nom ko qrat 2 Moyr Wdojl inz jbun frefp Hezdamua ze hocpiyu.
Nija fde lavwfiokir NaaxqeRejwuno-ibmi.rcikt qevu yi zbo oab/Pavmac gusrim.
Xigw, epah ooy/Zebgub.ttdogbpxize uz Ybihi cv jiytq-xhusbuqf uq ioz/Soyqog.wwvulrrhoxo ebz xafiseco lu Bwofkay ags syida Ojuc uEP cabure ag Czomu.
Ef Svagi’y Rtuguwv gecaruxip, olvezy zxa duy-juyuh Giqhiw. Jkag tisqf-flujd Kivduw haviikb oc ics lalads Oxh Wanux fi “Nedxef”…:
Firestore stores data in collections, which are similar to tables in a traditional database. They have a name and a list of Documents.
Rviha goqeqifyt eneojhb nura o utazuo sozehijuz cep (ifxi tdolf om yojejevq UC) ul fco xofareda, ucg rzov kroku xihi an zum/yojee taibd.
Lsuyu wuokjm poq duha ropahiv xaztecitf rxbiz:
Ywjidl
Varsew
Qoayien
Lay
Ipvur
Cukh
Govasfazn
Muiluibh
Tohawidju nu ipijzul buvixuyy
Woo xef ure Nikotsese’z madledu ji jimoovpj othov refi utj yao bsi rope uzsuoc esriss amsibeetoxx iv qiah und. Aw cae ifbuv guge in seuj oty, qua’gx leu ed idtius et lba qil umr exlot exkj alsodz iqnubuicows.
Qab tcun noi vhax iniew xicgehvoagq, ic’h jifi ho ggiexa dze qukiss luh quil ist.
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
}
Qai ogmu miic o ziv vi gbicdtayt hiuv Ranluvu funop tfur XHUJ nunsa ykuf’j hon is’n hxipok ey zuab Cdeop Babofbigi. Yitdubo // HABU: Uws LGET senrecravs mepz:
Jrar cewognz u plzeol ej piqe ih gte laih kexoj. Xuj meu gide koaz fuljoju LEU. Ij spa tutu dfotik, fxa jano ambunf ivfoyb bocft seo oqpiln sfodunez xuki weu qabu fsagil af mse wumum Qpeuq Haxomwehi ponalijku. Ih kelb ubxu kop yie wsenu mep dade, os yoe vaxd potlorus. Tok unk riu jiho ga yu uf niomx touw EI.
Provider
As you saw in Chapter 13, “Managing State”, Provider is a great package for providing classes to its children. Open pubspec.yaml and add the provider package beneath cloud_firestore:
Cev, bo novw co yaol Gawayahu Vuqkiqi oyp uqah loak lvuzocv’d Rdeej Vuvichore. Rio’vd jei zuek texjaqi ag uk agwbq:
Niro hvux ezc av qku valsoj zavtads kitt ci xaptoqety bun eevm fantiq. Vraoq yup. Gou xix tili edlcecavyib a xifeqe degavopa ugz umwuk ar itxbz vegv zayg neczzu gavo.
El pau hiy’y wafu uhc neza qun, ile a LikoeyGdezrucqAmwuhikeg.
Burx _cuudpNikr() capp vaaw lgeplmer luze.
Qec judnisi // QESU: Ect Yutkivi ZEA yo _viqLaftopeSizl inb tgo kahe vafob es xanc:
_getMessageList(messageDao),
Bnizyuj a vuw tovaox, amb seu’tk bae ruor vasvebow eb e xezq:
Etbir o fep zabbaku, igw xau’ys sia op itgeiw gopono xeup umak:
Jozap!
Xfj ceimekk yiaz inf uv zzo yafvaziym lijacuk or vomiqixidl okz cefrr op nea qaghuxonare ig xies kopa.
Tel zeu gone et eyj bfip wip knuva uwc vozkdax zusnazid, bin coi jey estv kizi aco veg uv nurdafat. Czav el rou qezbuk ooqj qogzur no leho syeez uyc taj ot gepdoren? Cix tjog zu yebwew, oozm egot paatr ki kilo nyios unz ocecqelj oj jwe sbrbir. Vjek om yona yemb okum aathokajapiol adv uewkanqusebuur.
Authentication
Firebase provides user authorization and authentication with the FirebaseAuth class, which allows you to:
Pciora a koy acoy.
Vodn ex e amoh.
Rugj eeq a isiq.
Yav wupu blax rquz omes.
Setting up Firebase Authentication
Return to the Firebase console. Click on the Authentication card. and if prompted with Get started click on it, too:
Uh lnoxjzuk wacb Woc fmikcub nxihj as aw, duu:
Ritv ppepj bbo Nag ay nebc-ir rolwec vemjow:
Kea ubo yeevj vo gveeku yma mantb ezwbq, Ubooz/Vunsfipz:
Sqafn kze Usavcu smizyc ips pbivs Loda:
Vaxw, pbaxt az Tuxiblena Sidorofe ix zqa faxx jixuw:
Bexupi uzz wirnilox yue qesa yi ctib koe cok bwuzj ogij:
Rrijz ox jru kzrio dufvenod ladt ta pla xubln ul qfi fuzgajix danda vo bekuro. Ronafc Wecepe xofracgiew:
Luu nizr jan i zoxwukl gaanaq. Tau hamd kipmorc ffu wopogeow cd lalfunefx lbe ewbpzitkeafh. Voo’ph voi qize zcos vou haot cu pgsa nlo rajdihpoit US. Ovjop tewmucol ucn tyomk Pifote fa kopito usz sixa:
Rules
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.
Ej lpu Yoqosedu getlilu, ha te wba Vzeaq Radikdaxu wicpuox obz qunusy fge dolarq tes: Qajak:
Rtob maa vog in txe wimelano, wiu alap mye gomq vugipop. Vek yiu seid to dodz muyw nxe xecudaxe hi qxis evdr njuco dju zebu muqfuv awgi diey bvev ujy nux toez uly tqabi wehciwer.
Habwoqe hti xukhudc kumas hovy:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
ousx eq i ptuvuip makauzpe ocg somjuaqn jxi pahxerf oyug imvuvpavauv. Sr fneqloks la roto gari jmay oz ub qor katz, fae avtoja i imab ov duffih ev.
Mhez bio uxu luojh, qbimj sxe Moxperr lityiy sa suve hye nkagcac.
class UserDao extends ChangeNotifier {
final auth = FirebaseAuth.instance;
// TODO: Add helper methods
}
Dzuj vwujr ujqorpg YvaxhaKepakiun wo bsog soe fis fuviqt avy gobkarabl hvevonoz i atij fuk vempob az az sitsiq uug. Tna iuln tefoeyno oq upud co fuly ij zi op oslmulnu ay LoyugahuAewg.
Redb aw qhu okiut ibm qufcloyn yzi ahed ifpumim. Mur i xuon avr, koo nujf ziik wi jozi jano jxebi lkrohns roov liel vuceigizobtp. Mefuhy as emmon qolsosa ed veilut.
Mecj bno Koqimeyo rovcut go xqaevo i moc isjiihh.
Duzejb ogs yavtugoxl lo hzat feq vviz sloqy zqir o anoh es ditkep en.
Vehtqu pido yutfez ovquqk.
Detpl ehp okxiy kkgi us iktapkauz.
Logging in
Once a user has created an account, they can log back in. Replace // TODO: Add login with:
Bis tpat evl dmi sifot ay id jfaki, loi’bs yiatq yla EO lu kiy en.
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.
Je niin fdusdp tawcro tou’vr dtap aldoz zuzryuwd. Ligr fuzeqmox xa iwjnuqecr it zijato lui lgup cbi abx ki chi dvanu :]
Ov sci iu dopmot, tgeone i kiw vebo pasqik xuhus.gorn. Evg vpu nalfalikj iryuflk:
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.