Skip to content

Commit 7594732

Browse files
authored
Get a document update (#14)
* show usage of doc.data * update city model
1 parent a4fa5b8 commit 7594732

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

packages/firebase_snippets_app/lib/model/firestore_add_data_custom_objects_snippet.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,20 @@ class City {
3232
this.regions,
3333
});
3434

35-
City.fromFirestore(
35+
factory City.fromFirestore(
3636
DocumentSnapshot<Map<String, dynamic>> snapshot,
3737
SnapshotOptions? options,
38-
) : name = snapshot.data()?["name"],
39-
state = snapshot.data()?["state"],
40-
country = snapshot.data()?["country"],
41-
capital = snapshot.data()?["capital"],
42-
population = snapshot.data()?["population"],
43-
regions =
44-
(snapshot.data()?["regions"] as List<dynamic>?)?.cast<String>();
38+
) {
39+
final data = snapshot.data() as Map<String, dynamic>;
40+
return City(
41+
name: data['name'],
42+
state: data['state'],
43+
country: data['country'],
44+
capital: data['capital'],
45+
population: data['population'],
46+
regions: data['regions'] as List<String>,
47+
);
48+
}
4549

4650
Map<String, dynamic> toFirestore() {
4751
return {

packages/firebase_snippets_app/lib/snippets/firestore.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,12 @@ class FirestoreSnippets extends DocSnippet {
411411
// [START get_data_once_get_a_document]
412412
final docRef = db.collection("cities").doc("SF");
413413
docRef.get().then(
414-
(res) => print("Successfully completed"),
415-
onError: (e) => print("Error completing: $e"),
416-
);
414+
(DocumentSnapshot doc) {
415+
final data = doc.data() as Map<String, dynamic>;
416+
// ...
417+
},
418+
onError: (e) => print("Error getting document: $e"),
419+
);
417420
// [END get_data_once_get_a_document]
418421
}
419422

0 commit comments

Comments
 (0)