Skip to content

Commit e5e0944

Browse files
Add custom object example (#12)
1 parent a16acb6 commit e5e0944

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

packages/firebase_snippets_app/lib/model/firestore_add_data_custom_objects_snippet.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import 'package:cloud_firestore/cloud_firestore.dart';
16+
17+
// [START add_data_custom_objects]
1518
class City {
1619
final String? name;
1720
final String? state;
@@ -29,6 +32,16 @@ class City {
2932
this.regions,
3033
});
3134

35+
City.fromFirestore(
36+
DocumentSnapshot<Map<String, dynamic>> snapshot,
37+
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 = snapshot.data()?["regions"];
44+
3245
Map<String, dynamic> toFirestore() {
3346
return {
3447
if (name != null) "name": name,
@@ -40,3 +53,4 @@ class City {
4053
};
4154
}
4255
}
56+
// [END add_data_custom_objects]

packages/firebase_snippets_app/lib/snippets/firestore.dart

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -158,22 +158,8 @@ class FirestoreSnippets extends DocSnippet {
158158
// [END add_data_data_types]
159159
}
160160

161-
void addData_customObjects() {
162-
// [START add_data_custom_objects]
163-
// TODO: is this supported in Dart yet?
164-
// in the current docs, it isn't supported for about half the languages
165-
// if it is, it will look like the code in [./custom_snippets/firestore_add_data_custom_objects_snippet.dart]
166-
167-
// This is how it's done in some other languages, but this doesn't seem like it should be included,
168-
// because it isn't really a Firebase feature. I'm just converting Objects into a Map before submitting it.
169-
}
170-
171-
void addData_customObjects2() {
161+
void addData_customObjects2() async {
172162
// [START add_data_custom_objects2]
173-
// TODO: is this supported in Dart yet?
174-
// in the current docs, it isn't supported for about half the languages
175-
// See note above before including this in the docs.
176-
177163
final city = City(
178164
name: "Los Angeles",
179165
state: "CA",
@@ -182,8 +168,14 @@ class FirestoreSnippets extends DocSnippet {
182168
population: 5000000,
183169
regions: ["west_coast", "socal"],
184170
);
185-
186-
db.collection("cities").doc("LA").set(city.toFirestore());
171+
final docRef = db
172+
.collection("cities")
173+
.withConverter(
174+
fromFirestore: City.fromFirestore,
175+
toFirestore: (City city, options) => city.toFirestore(),
176+
)
177+
.doc("LA");
178+
await docRef.set(city);
187179
// [END add_data_custom_objects2]
188180
}
189181

@@ -439,17 +431,19 @@ class FirestoreSnippets extends DocSnippet {
439431
// [END get_data_once_source_options]
440432
}
441433

442-
void getDataOnce_customObjects() {
443-
// TODO: should this be included?
444-
// This isn't really specific to Firestore, it's just serdes
434+
void getDataOnce_customObjects() async {
445435
// [START get_data_once_custom_objects]
446-
final docRef = db.collection("cities").doc("BJ");
447-
docRef.get().then((documentSnapshot) {
448-
final data = documentSnapshot.data() as Map<String, dynamic>;
449-
final city = City(
450-
name: data['name'],
451-
);
452-
});
436+
final ref = db.collection("cities").doc("LA").withConverter(
437+
fromFirestore: City.fromFirestore,
438+
toFirestore: (City city, _) => city.toFirestore(),
439+
);
440+
final docSnap = await ref.get();
441+
final city = docSnap.data(); // Convert to City object
442+
if (city != null) {
443+
print(city);
444+
} else {
445+
print("No such document.");
446+
}
453447
// [END get_data_once_custom_objects]
454448
}
455449

@@ -917,7 +911,8 @@ class FirestoreSnippets extends DocSnippet {
917911
db.settings = const Settings(persistenceEnabled: true);
918912

919913
// Web
920-
await db.enablePersistence(const PersistenceSettings(synchronizeTabs: true));
914+
await db
915+
.enablePersistence(const PersistenceSettings(synchronizeTabs: true));
921916
// [END access_data_offline_configure_offline_persistence]
922917
}
923918

0 commit comments

Comments
 (0)