-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.dart
133 lines (116 loc) · 3.58 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firestore_cache/firestore_cache.dart';
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firestore Cache Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
MyHomePageState createState() => MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
final _firestore = FirebaseFirestore.instance;
late Future<DocumentSnapshot<Map<String, dynamic>>> _futureDoc;
late Future<QuerySnapshot<Map<String, dynamic>>> _futureSnapshot;
@override
void initState() {
super.initState();
_futureDoc = _getDoc();
_futureSnapshot = _getDocs();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Firestore Cache Demo')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
const Text('Get single document'),
_buildDoc(),
const Divider(),
const Text('Get collection of documents'),
_buildDocs(),
],
),
),
);
}
Widget _buildDoc() {
return FutureBuilder<DocumentSnapshot>(
future: _futureDoc,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text('${snapshot.error}');
} else if (snapshot.hasData) {
final doc = snapshot.data!;
final data = doc.data() as Map?;
return Text(
'${data!['userId']} isFromCache: ${doc.metadata.isFromCache}',
);
}
return const CircularProgressIndicator();
},
);
}
Widget _buildDocs() {
return FutureBuilder<QuerySnapshot>(
future: _futureSnapshot,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text('${snapshot.error}');
} else if (snapshot.hasData) {
final docs = snapshot.data?.docs;
return Expanded(
child: ListView(
children: docs!.map((DocumentSnapshot doc) {
final data = doc.data() as Map?;
return Text(
'${data!['postId']} isFromCache: ${doc.metadata.isFromCache}',
textAlign: TextAlign.center,
);
}).toList(),
),
);
}
return const CircularProgressIndicator();
},
);
}
Future<DocumentSnapshot<Map<String, dynamic>>> _getDoc() async {
final docRef = _firestore.doc('users/user');
final doc = await FirestoreCache.getDocument(docRef);
return doc;
}
Future<QuerySnapshot<Map<String, dynamic>>> _getDocs() async {
const cacheField = 'updatedAt';
final cacheDocRef = _firestore.doc('status/status');
final query = _firestore.collection('posts');
final snapshot = await FirestoreCache.getDocuments(
query: query,
cacheDocRef: cacheDocRef,
firestoreCacheField: cacheField,
);
return snapshot;
}
}