Skip to content

Commit 0e57037

Browse files
authored
Add examples for new Flow operators in firestore-ktx (#4078)
1 parent 54ab04e commit 0e57037

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

docs/ktx/firestore.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,64 @@ val firestore = Firebase.firestore
2727
val anotherFirestore = Firebase.firestore(Firebase.app("myApp"))
2828
```
2929

30+
### Get a document
31+
32+
**Kotlin**
33+
```kotlin
34+
firestore.collection("cities")
35+
.document("LON")
36+
.addSnapshotListener { document: DocumentSnapshot?, error: ->
37+
if (error != null) {
38+
// Handle error
39+
return@addSnapshotListener
40+
}
41+
if (document != null) {
42+
// Use document
43+
}
44+
}
45+
```
46+
47+
**Kotlin + KTX**
48+
```kotlin
49+
firestore.collection("cities")
50+
.document("LON")
51+
.snapshots()
52+
.collect { document: DocumentSnapshot ->
53+
// Use document
54+
}
55+
```
56+
57+
### Query documents
58+
59+
**Kotlin**
60+
```kotlin
61+
firestore.collection("cities")
62+
.whereEqualTo("capital", true)
63+
.addSnapshotListener { documents: QuerySnapshot?, error ->
64+
if (error != null) {
65+
// Handle error
66+
return@addSnapshotListener
67+
}
68+
if (documents != null) {
69+
for (document in documents) {
70+
// Use document
71+
}
72+
}
73+
}
74+
```
75+
76+
**Kotlin + KTX**
77+
```kotlin
78+
firestore.collection("cities")
79+
.whereEqualTo("capital", true)
80+
.snapshots()
81+
.collect { documents: QuerySnapshot ->
82+
for (document in documents) {
83+
// Use document
84+
}
85+
}
86+
```
87+
3088
### Convert a DocumentSnapshot field to a POJO
3189

3290
**Kotlin**

0 commit comments

Comments
 (0)