Skip to content

Commit 7b6cc17

Browse files
authored
adds snippets for ODM (#8)
1 parent 0c43714 commit 7b6cc17

File tree

8 files changed

+472
-23
lines changed

8 files changed

+472
-23
lines changed

packages/firebase_snippets_app/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
.pub-cache/
3131
.pub/
3232
/build/
33-
*.g.dart
3433

3534
# Web related
3635
lib/generated_plugin_registrant.dart
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// ignore_for_file: non_constant_identifier_names
2+
3+
import 'package:firebase_snippets_app/snippets/firestore_odm/user_model.dart';
4+
import 'package:firebase_snippets_app/snippets/snippet_base.dart';
5+
6+
class ODMSnippets extends DocSnippet {
7+
@override
8+
void runAll() {
9+
// TODO: implement runAll
10+
}
11+
12+
void references_performQueries() async {
13+
// [START references_perform_queries]
14+
usersRef.whereName(isEqualTo: 'John');
15+
usersRef.whereAge(isGreaterThan: 18);
16+
usersRef.orderByAge();
17+
// ..etc!
18+
// [END references_perform_queries]
19+
}
20+
21+
void subCollection_accessSubCollection() async {
22+
// [START sub_collection_access_sub_collection]
23+
AddressCollectionReference addressesRef =
24+
usersRef.doc('myDocumentID').addresses;
25+
// [END sub_collection_access_sub_collection]
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
3+
part 'address_model.g.dart';
4+
5+
// [START sub_collections_define]
6+
@JsonSerializable()
7+
class Address {
8+
final String streetName;
9+
10+
Address({required this.streetName});
11+
12+
factory Address.fromJson(Map<String, Object?> json) =>
13+
_$AddressFromJson(json);
14+
15+
Map<String, Object?> toJson() => _$AddressToJson(this);
16+
}
17+
// [END sub_collections_define]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// ignore_for_file: non_constant_identifier_names
2+
3+
import 'package:cloud_firestore/cloud_firestore.dart';
4+
import 'package:cloud_firestore_odm/cloud_firestore_odm.dart';
5+
import 'package:firebase_snippets_app/snippets/firestore_odm/address_model.dart';
6+
import 'package:json_annotation/json_annotation.dart';
7+
8+
part 'user_model.g.dart';
9+
10+
@JsonSerializable()
11+
class User {
12+
User({
13+
required this.name,
14+
required this.age,
15+
required this.email,
16+
required this.address,
17+
}) {
18+
_$assertUser(this);
19+
}
20+
21+
factory User.fromJson(Map<String, Object?> json) => _$UserFromJson(json);
22+
23+
final String name;
24+
final String email;
25+
final Address address;
26+
27+
@Min(0)
28+
final int age;
29+
30+
Map<String, Object?> toJson() => _$UserToJson(this);
31+
}
32+
// [END defining_models]
33+
34+
// [START references_collection_ref]
35+
@Collection<User>('users')
36+
@Collection<Address>('users/*/addresses')
37+
final usersRef = UserCollectionReference();
38+
// [END references_collection_ref]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// ignore_for_file: use_key_in_widget_constructors
2+
3+
import 'package:cloud_firestore_odm/cloud_firestore_odm.dart';
4+
import 'package:flutter/material.dart';
5+
6+
import '../snippets/firestore_odm/user_model.dart';
7+
8+
// Not currently in the docs, so not currently being tested.
9+
class UserLabel extends StatelessWidget {
10+
const UserLabel(this.id);
11+
12+
final String id;
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return FirestoreBuilder<UserDocumentSnapshot>(
17+
// Access a specific document
18+
ref: usersRef.doc(id),
19+
builder: (context, AsyncSnapshot<UserDocumentSnapshot> snapshot,
20+
Widget? child) {
21+
if (snapshot.hasError) return const Text('Something went wrong!');
22+
if (!snapshot.hasData) return const Text('Loading user...');
23+
24+
// Access the UserDocumentSnapshot
25+
UserDocumentSnapshot documentSnapshot = snapshot.requireData;
26+
27+
if (!documentSnapshot.exists) {
28+
return const Text('User does not exist.');
29+
}
30+
31+
User user = documentSnapshot.data!;
32+
33+
return Text('User name: ${user.name}, age ${user.age}');
34+
});
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'package:cloud_firestore_odm/cloud_firestore_odm.dart';
2+
import 'package:flutter/material.dart';
3+
4+
import '../snippets/firestore_odm/user_model.dart';
5+
6+
// Not currently in the docs, so not currently being tested.
7+
class UsersList extends StatelessWidget {
8+
const UsersList({Key? key}) : super(key: key);
9+
10+
@override
11+
Widget build(BuildContext context) {
12+
return FirestoreBuilder<UserQuerySnapshot>(
13+
ref: usersRef,
14+
builder: (context, AsyncSnapshot<UserQuerySnapshot> snapshot,
15+
Widget? child) {
16+
if (snapshot.hasError) return const Text('Something went wrong!');
17+
if (!snapshot.hasData) return const Text('Loading users...');
18+
19+
// Access the QuerySnapshot
20+
UserQuerySnapshot querySnapshot = snapshot.requireData;
21+
22+
return ListView.builder(
23+
itemCount: querySnapshot.docs.length,
24+
itemBuilder: (context, index) {
25+
// Access the User instance
26+
User user = querySnapshot.docs[index].data;
27+
28+
return Text('User name: ${user.name}, age ${user.age}');
29+
},
30+
);
31+
});
32+
}
33+
}

0 commit comments

Comments
 (0)