Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/firebase_snippets_app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
.pub-cache/
.pub/
/build/
*.g.dart

# Web related
lib/generated_plugin_registrant.dart
Expand Down
27 changes: 27 additions & 0 deletions packages/firebase_snippets_app/lib/snippets/firestore_odm.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ignore_for_file: non_constant_identifier_names

import 'package:firebase_snippets_app/snippets/firestore_odm/user_model.dart';
import 'package:firebase_snippets_app/snippets/snippet_base.dart';

class ODMSnippets extends DocSnippet {
@override
void runAll() {
// TODO: implement runAll
}

void references_performQueries() async {
// [START references_perform_queries]
usersRef.whereName(isEqualTo: 'John');
usersRef.whereAge(isGreaterThan: 18);
usersRef.orderByAge();
// ..etc!
// [END references_perform_queries]
}

void subCollection_accessSubCollection() async {
// [START sub_collection_access_sub_collection]
AddressCollectionReference addressesRef =
usersRef.doc('myDocumentID').addresses;
// [END sub_collection_access_sub_collection]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:json_annotation/json_annotation.dart';

part 'address_model.g.dart';

// [START sub_collections_define]
@JsonSerializable()
class Address {
final String streetName;

Address({required this.streetName});

factory Address.fromJson(Map<String, Object?> json) =>
_$AddressFromJson(json);

Map<String, Object?> toJson() => _$AddressToJson(this);
}
// [END sub_collections_define]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// ignore_for_file: non_constant_identifier_names

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_firestore_odm/cloud_firestore_odm.dart';
import 'package:firebase_snippets_app/snippets/firestore_odm/address_model.dart';
import 'package:json_annotation/json_annotation.dart';

part 'user_model.g.dart';

@JsonSerializable()
class User {
User({
required this.name,
required this.age,
required this.email,
required this.address,
}) {
_$assertUser(this);
}

factory User.fromJson(Map<String, Object?> json) => _$UserFromJson(json);

final String name;
final String email;
final Address address;

@Min(0)
final int age;

Map<String, Object?> toJson() => _$UserToJson(this);
}
// [END defining_models]

// [START references_collection_ref]
@Collection<User>('users')
@Collection<Address>('users/*/addresses')
final usersRef = UserCollectionReference();
// [END references_collection_ref]
36 changes: 36 additions & 0 deletions packages/firebase_snippets_app/lib/widgets/user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// ignore_for_file: use_key_in_widget_constructors

import 'package:cloud_firestore_odm/cloud_firestore_odm.dart';
import 'package:flutter/material.dart';

import '../snippets/firestore_odm/user_model.dart';

// Not currently in the docs, so not currently being tested.
class UserLabel extends StatelessWidget {
const UserLabel(this.id);

final String id;

@override
Widget build(BuildContext context) {
return FirestoreBuilder<UserDocumentSnapshot>(
// Access a specific document
ref: usersRef.doc(id),
builder: (context, AsyncSnapshot<UserDocumentSnapshot> snapshot,
Widget? child) {
if (snapshot.hasError) return const Text('Something went wrong!');
if (!snapshot.hasData) return const Text('Loading user...');

// Access the UserDocumentSnapshot
UserDocumentSnapshot documentSnapshot = snapshot.requireData;

if (!documentSnapshot.exists) {
return const Text('User does not exist.');
}

User user = documentSnapshot.data!;

return Text('User name: ${user.name}, age ${user.age}');
});
}
}
33 changes: 33 additions & 0 deletions packages/firebase_snippets_app/lib/widgets/users_list.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:cloud_firestore_odm/cloud_firestore_odm.dart';
import 'package:flutter/material.dart';

import '../snippets/firestore_odm/user_model.dart';

// Not currently in the docs, so not currently being tested.
class UsersList extends StatelessWidget {
const UsersList({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return FirestoreBuilder<UserQuerySnapshot>(
ref: usersRef,
builder: (context, AsyncSnapshot<UserQuerySnapshot> snapshot,
Widget? child) {
if (snapshot.hasError) return const Text('Something went wrong!');
if (!snapshot.hasData) return const Text('Loading users...');

// Access the QuerySnapshot
UserQuerySnapshot querySnapshot = snapshot.requireData;

return ListView.builder(
itemCount: querySnapshot.docs.length,
itemBuilder: (context, index) {
// Access the User instance
User user = querySnapshot.docs[index].data;

return Text('User name: ${user.name}, age ${user.age}');
},
);
});
}
}
Loading