-
Notifications
You must be signed in to change notification settings - Fork 11
/
firestore_add_data_custom_objects_snippet.dart
62 lines (57 loc) · 1.73 KB
/
firestore_add_data_custom_objects_snippet.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'package:cloud_firestore/cloud_firestore.dart';
// [START add_data_custom_objects]
class City {
final String? name;
final String? state;
final String? country;
final bool? capital;
final int? population;
final List<String>? regions;
City({
this.name,
this.state,
this.country,
this.capital,
this.population,
this.regions,
});
factory City.fromFirestore(
DocumentSnapshot<Map<String, dynamic>> snapshot,
SnapshotOptions? options,
) {
final data = snapshot.data();
return City(
name: data?['name'],
state: data?['state'],
country: data?['country'],
capital: data?['capital'],
population: data?['population'],
regions:
data?['regions'] is Iterable ? List.from(data?['regions']) : null,
);
}
Map<String, dynamic> toFirestore() {
return {
if (name != null) "name": name,
if (state != null) "state": state,
if (country != null) "country": country,
if (capital != null) "capital": capital,
if (population != null) "population": population,
if (regions != null) "regions": regions,
};
}
}
// [END add_data_custom_objects]