Skip to content

Commit 59be871

Browse files
Devs@HathTech.comDevs@HathTech.com
authored andcommitted
added code to add and get data from firestore
1 parent 9d1dc11 commit 59be871

File tree

3 files changed

+152
-100
lines changed

3 files changed

+152
-100
lines changed

.flutter-plugins-dependencies

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"cloud_firestore","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.16.0/","dependencies":["firebase_core"]},{"name":"firebase_auth","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_auth-0.20.0+1/","dependencies":["firebase_core"]},{"name":"firebase_core","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_core-0.7.0/","dependencies":[]},{"name":"fluttertoast","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/fluttertoast-7.1.6/","dependencies":[]}],"android":[{"name":"cloud_firestore","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.16.0/","dependencies":["firebase_core"]},{"name":"firebase_auth","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_auth-0.20.0+1/","dependencies":["firebase_core"]},{"name":"firebase_core","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_core-0.7.0/","dependencies":[]},{"name":"fluttertoast","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/fluttertoast-7.1.6/","dependencies":[]}],"macos":[{"name":"cloud_firestore","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.16.0/","dependencies":["firebase_core"]},{"name":"firebase_auth","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_auth-0.20.0+1/","dependencies":["firebase_core"]},{"name":"firebase_core","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_core-0.7.0/","dependencies":[]}],"linux":[],"windows":[],"web":[{"name":"cloud_firestore_web","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/cloud_firestore_web-0.3.0+1/","dependencies":["firebase_core_web"]},{"name":"firebase_auth_web","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_auth_web-0.3.2+6/","dependencies":["firebase_core_web"]},{"name":"firebase_core_web","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-0.2.1+3/","dependencies":[]},{"name":"fluttertoast","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/fluttertoast-7.1.6/","dependencies":[]}]},"dependencyGraph":[{"name":"cloud_firestore","dependencies":["firebase_core","cloud_firestore_web"]},{"name":"cloud_firestore_web","dependencies":["firebase_core","firebase_core_web"]},{"name":"firebase_auth","dependencies":["firebase_core","firebase_auth_web"]},{"name":"firebase_auth_web","dependencies":["firebase_core","firebase_core_web"]},{"name":"firebase_core","dependencies":["firebase_core_web"]},{"name":"firebase_core_web","dependencies":[]},{"name":"fluttertoast","dependencies":[]}],"date_created":"2021-01-22 16:09:18.400548","version":"1.25.0-8.3.pre"}
1+
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"cloud_firestore","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.16.0/","dependencies":["firebase_core"]},{"name":"firebase_auth","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_auth-0.20.0+1/","dependencies":["firebase_core"]},{"name":"firebase_core","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_core-0.7.0/","dependencies":[]},{"name":"fluttertoast","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/fluttertoast-7.1.6/","dependencies":[]}],"android":[{"name":"cloud_firestore","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.16.0/","dependencies":["firebase_core"]},{"name":"firebase_auth","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_auth-0.20.0+1/","dependencies":["firebase_core"]},{"name":"firebase_core","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_core-0.7.0/","dependencies":[]},{"name":"fluttertoast","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/fluttertoast-7.1.6/","dependencies":[]}],"macos":[{"name":"cloud_firestore","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.16.0/","dependencies":["firebase_core"]},{"name":"firebase_auth","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_auth-0.20.0+1/","dependencies":["firebase_core"]},{"name":"firebase_core","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_core-0.7.0/","dependencies":[]}],"linux":[],"windows":[],"web":[{"name":"cloud_firestore_web","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/cloud_firestore_web-0.3.0+1/","dependencies":["firebase_core_web"]},{"name":"firebase_auth_web","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_auth_web-0.3.2+6/","dependencies":["firebase_core_web"]},{"name":"firebase_core_web","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-0.2.1+3/","dependencies":[]},{"name":"fluttertoast","path":"/Users/rb_mac_min/.pub-cache/hosted/pub.dartlang.org/fluttertoast-7.1.6/","dependencies":[]}]},"dependencyGraph":[{"name":"cloud_firestore","dependencies":["firebase_core","cloud_firestore_web"]},{"name":"cloud_firestore_web","dependencies":["firebase_core","firebase_core_web"]},{"name":"firebase_auth","dependencies":["firebase_core","firebase_auth_web"]},{"name":"firebase_auth_web","dependencies":["firebase_core","firebase_core_web"]},{"name":"firebase_core","dependencies":["firebase_core_web"]},{"name":"firebase_core_web","dependencies":[]},{"name":"fluttertoast","dependencies":[]}],"date_created":"2021-01-23 09:32:24.949427","version":"1.25.0-8.3.pre"}

lib/home.dart

Lines changed: 151 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,131 @@ import 'package:firebase_tutorials/app.dart';
44
import 'package:firebase_tutorials/model.dart';
55
import 'package:flutter/material.dart';
66

7-
class HomePage extends StatelessWidget {
7+
class HomePage extends StatefulWidget {
88
final User user;
9-
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
10-
119
HomePage({Key key, @required this.user})
1210
: assert(user != null),
1311
super(key: key);
12+
@override
13+
_HomePageState createState() => _HomePageState();
14+
}
15+
16+
class _HomePageState extends State<HomePage> {
17+
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
18+
19+
List<ItemModel> items;
20+
String errorMessage;
1421

1522
@override
16-
Widget build(BuildContext context) {
23+
void initState() {
24+
super.initState();
25+
if (!mounted) return;
26+
// getItemsStream();
27+
getItemsByFilter();
28+
}
29+
30+
void getItemsStream() {
31+
CollectionReference itemRef =
32+
FirebaseFirestore.instance.collection('items');
33+
34+
itemRef.snapshots().listen((event) {
35+
if (event != null) {
36+
setState(() {
37+
items = event.docs.map((e) => ItemModel.fromFirestore(e)).toList();
38+
});
39+
}
40+
}, onError: (e) {
41+
setState(() {
42+
errorMessage = e.toString();
43+
});
44+
});
45+
}
46+
47+
void getItemsFuture() async {
48+
try {
49+
CollectionReference itemRef =
50+
FirebaseFirestore.instance.collection('items');
51+
final data = await itemRef.get();
52+
if (data != null) {
53+
setState(() {
54+
items = data.docs.map((e) => ItemModel.fromFirestore(e)).toList();
55+
});
56+
}
57+
} catch (e) {
58+
setState(() {
59+
errorMessage = e.toString();
60+
});
61+
}
62+
}
63+
64+
void getItemsByFilter() {
65+
// HERE I am using user id for filter but you can use any variable
1766
CollectionReference itemRef =
1867
FirebaseFirestore.instance.collection('items');
1968

69+
itemRef.where('userId', isEqualTo: widget.user.uid).snapshots().listen(
70+
(event) {
71+
if (event != null) {
72+
setState(() {
73+
items = event.docs.map((e) => ItemModel.fromFirestore(e)).toList();
74+
});
75+
}
76+
}, onError: (e) {
77+
setState(() {
78+
errorMessage = e.toString();
79+
});
80+
});
81+
}
82+
83+
void getItemsInSortedOrder() {
84+
// You can sort the record by date and time along with filter
85+
// but filter you might have to create any index
86+
CollectionReference itemRef =
87+
FirebaseFirestore.instance.collection('items');
88+
89+
itemRef
90+
.where('userId', isEqualTo: widget.user.uid)
91+
.where('createdAt',
92+
isGreaterThan: DateTime.now().subtract(Duration(days: 10)))
93+
.snapshots()
94+
.listen((event) {
95+
if (event != null) {
96+
setState(() {
97+
items = event.docs.map((e) => ItemModel.fromFirestore(e)).toList();
98+
});
99+
}
100+
}, onError: (e) {
101+
setState(() {
102+
errorMessage = e.toString();
103+
});
104+
});
105+
}
106+
107+
void addItem(ItemModel item) async {
108+
try {
109+
final userId = widget.user.uid;
110+
CollectionReference itemRef =
111+
FirebaseFirestore.instance.collection('items');
112+
113+
// I am showing you without form but you got it how you can add the data to firestore
114+
await itemRef.add({
115+
'userId': userId,
116+
'name': item.name,
117+
'description': item.description,
118+
'deleted': false,
119+
'id': itemRef.id,
120+
'status': true,
121+
'featured': item.featured,
122+
'createdAt': DateTime.now()
123+
});
124+
} catch (e) {
125+
// You can use scaffold to show error
126+
print(e.toString());
127+
}
128+
}
129+
130+
@override
131+
Widget build(BuildContext context) {
20132
return Scaffold(
21133
appBar: AppBar(
22134
title: Text('Home Screen'),
@@ -36,53 +148,51 @@ class HomePage extends StatelessWidget {
36148
),
37149
floatingActionButton: FloatingActionButton(
38150
child: Icon(Icons.add),
39-
onPressed: () {},
151+
onPressed: () {
152+
addItem(
153+
ItemModel(name: 'Test Item', description: 'Test Description'));
154+
},
40155
),
41156
body: SingleChildScrollView(
42157
child: Padding(
43158
padding: const EdgeInsets.all(8.0),
44159
child: Column(
45-
crossAxisAlignment: CrossAxisAlignment.start,
46-
children: <Widget>[
47-
SizedBox(
48-
height: 16,
49-
),
50-
Center(
51-
child: Text(user.phoneNumber),
52-
),
53-
Text(
54-
'Items',
55-
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
56-
),
57-
StreamBuilder<QuerySnapshot>(
58-
stream: itemRef.snapshots(),
59-
builder: (BuildContext context,
60-
AsyncSnapshot<QuerySnapshot> snapshot) {
61-
if (snapshot.hasError) {
62-
return Text('Something went wrong');
63-
}
64-
65-
if (snapshot.connectionState == ConnectionState.waiting) {
66-
return Text("Loading");
67-
}
68-
69-
return new ListView(
160+
crossAxisAlignment: CrossAxisAlignment.start,
161+
children: <Widget>[
162+
SizedBox(
163+
height: 16,
164+
),
165+
Center(
166+
child: Text(widget.user.phoneNumber),
167+
),
168+
Text(
169+
'Items',
170+
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
171+
),
172+
if (errorMessage != null) ...[
173+
SizedBox(
174+
height: 8,
175+
),
176+
Text(errorMessage ?? 'Something Went Wrong')
177+
] else if (items != null) ...[
178+
ListView.builder(
179+
itemCount: items.length,
70180
shrinkWrap: true,
71181
physics: NeverScrollableScrollPhysics(),
72-
children:
73-
snapshot.data.docs.map((DocumentSnapshot document) {
74-
final ItemModel item = ItemModel.fromFirestore(document);
75-
76-
return new ListTile(
182+
itemBuilder: (BuildContext context, int index) {
183+
final ItemModel item = items[index];
184+
return ListTile(
77185
title: new Text(item.name),
78186
subtitle: new Text(item.description),
79187
);
80-
}).toList(),
81-
);
82-
},
83-
)
84-
],
85-
),
188+
},
189+
),
190+
] else ...[
191+
Center(
192+
child: CircularProgressIndicator(),
193+
)
194+
]
195+
]),
86196
),
87197
),
88198
);

lib/list.dart

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)