@@ -158,22 +158,8 @@ class FirestoreSnippets extends DocSnippet {
158
158
// [END add_data_data_types]
159
159
}
160
160
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 {
172
162
// [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
-
177
163
final city = City (
178
164
name: "Los Angeles" ,
179
165
state: "CA" ,
@@ -182,8 +168,14 @@ class FirestoreSnippets extends DocSnippet {
182
168
population: 5000000 ,
183
169
regions: ["west_coast" , "socal" ],
184
170
);
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);
187
179
// [END add_data_custom_objects2]
188
180
}
189
181
@@ -439,17 +431,19 @@ class FirestoreSnippets extends DocSnippet {
439
431
// [END get_data_once_source_options]
440
432
}
441
433
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 {
445
435
// [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
+ }
453
447
// [END get_data_once_custom_objects]
454
448
}
455
449
@@ -917,7 +911,8 @@ class FirestoreSnippets extends DocSnippet {
917
911
db.settings = const Settings (persistenceEnabled: true );
918
912
919
913
// Web
920
- await db.enablePersistence (const PersistenceSettings (synchronizeTabs: true ));
914
+ await db
915
+ .enablePersistence (const PersistenceSettings (synchronizeTabs: true ));
921
916
// [END access_data_offline_configure_offline_persistence]
922
917
}
923
918
0 commit comments