Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9d1dc11

Browse files
Devs@HathTech.comDevs@HathTech.com
Devs@HathTech.com
authored and
Devs@HathTech.com
committed
Get data from firestore and list
1 parent 2a55814 commit 9d1dc11

File tree

6 files changed

+209
-14
lines changed

6 files changed

+209
-14
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月21日 16:58:33.764361","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月22日 16:09:18.400548","version":"1.25.0-8.3.pre"}

‎Firestore.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
### How to add firestore to flutter apps
2+
3+
#### Follow the video tutorial to create and add firebase project
4+
5+
Create firestore db and add firestore security rules
6+
7+
- Update the rules with below code
8+
- You can learn more about firebase firestore security rules [here](https://firebase.google.com/docs/firestore/security/get-started)
9+
10+
- Below rules states that you can read and write to firestore only if you are authenticated.
11+
12+
```
13+
rules_version = '2';
14+
service cloud.firestore {
15+
match /databases/{database}/documents {
16+
match /{document=**} {
17+
allow read, write: if request.auth.uid != null;
18+
}
19+
}
20+
}
21+
```
22+
23+
### Let's create a form to add data to firestore
24+
25+
- See file form.dart
26+
27+
### Complete below task
28+
29+
- Function to add data to firestore
30+
- Function to get data from firestore (by user id) Stream
31+
- Function to get data from firestore (by user id) Future
32+
- Sort and Filter

‎lib/home.dart

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
12
import 'package:firebase_auth/firebase_auth.dart';
23
import 'package:firebase_tutorials/app.dart';
4+
import 'package:firebase_tutorials/model.dart';
35
import 'package:flutter/material.dart';
46

57
class HomePage extends StatelessWidget {
@@ -12,24 +14,76 @@ class HomePage extends StatelessWidget {
1214

1315
@override
1416
Widget build(BuildContext context) {
17+
CollectionReference itemRef =
18+
FirebaseFirestore.instance.collection('items');
19+
1520
return Scaffold(
1621
appBar: AppBar(
1722
title: Text('Home Screen'),
23+
actions: [
24+
IconButton(
25+
icon: Icon(Icons.logout),
26+
onPressed: () {
27+
_firebaseAuth.signOut();
28+
Navigator.push(
29+
context,
30+
MaterialPageRoute(
31+
builder: (context) => App(),
32+
),
33+
);
34+
})
35+
],
1836
),
19-
body: Column(
20-
children: <Widget>[
21-
RaisedButton(
22-
child: Text("Sign out"),
23-
onPressed: () {
24-
_firebaseAuth.signOut();
25-
Navigator.push(
26-
context, MaterialPageRoute(builder: (context) => App()));
27-
},
28-
),
29-
Center(
30-
child: Text(user.phoneNumber),
37+
floatingActionButton: FloatingActionButton(
38+
child: Icon(Icons.add),
39+
onPressed: () {},
40+
),
41+
body: SingleChildScrollView(
42+
child: Padding(
43+
padding: const EdgeInsets.all(8.0),
44+
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(
70+
shrinkWrap: true,
71+
physics: NeverScrollableScrollPhysics(),
72+
children:
73+
snapshot.data.docs.map((DocumentSnapshot document) {
74+
final ItemModel item = ItemModel.fromFirestore(document);
75+
76+
return new ListTile(
77+
title: new Text(item.name),
78+
subtitle: new Text(item.description),
79+
);
80+
}).toList(),
81+
);
82+
},
83+
)
84+
],
3185
),
32-
],
86+
),
3387
),
3488
);
3589
}

‎lib/list.dart

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
2+
import 'package:flutter/material.dart';
3+
4+
class CategoryAdmin extends StatelessWidget {
5+
const CategoryAdmin({Key key}) : super(key: key);
6+
7+
@override
8+
Widget build(BuildContext context) {
9+
return Scaffold(
10+
appBar: AppBar(
11+
leading: IconButton(
12+
color: Colors.white,
13+
onPressed: () => Navigator.of(context).pop(),
14+
icon: Icon(Icons.arrow_back_ios, color: Colors.white),
15+
),
16+
title: Text(
17+
"Category",
18+
style: TextStyle(
19+
color: Colors.white, fontSize: 24.0, letterSpacing: 1.0),
20+
),
21+
),
22+
body: SingleChildScrollView(
23+
child: Column(
24+
children: <Widget>[
25+
Container(
26+
margin: EdgeInsets.symmetric(vertical: 16.0, horizontal: 54.0),
27+
child: Material(
28+
elevation: 8.0,
29+
color: Theme.of(context).primaryColor,
30+
borderRadius: BorderRadius.circular(0.0),
31+
child: InkWell(
32+
onTap: () {},
33+
child: Padding(
34+
padding: EdgeInsets.all(12.0),
35+
child: Row(
36+
mainAxisSize: MainAxisSize.min,
37+
mainAxisAlignment: MainAxisAlignment.center,
38+
crossAxisAlignment: CrossAxisAlignment.center,
39+
children: <Widget>[
40+
Icon(Icons.add, color: Colors.white),
41+
Padding(padding: EdgeInsets.only(right: 16.0)),
42+
Text(
43+
'ADD ITEM',
44+
style: TextStyle(
45+
color: Colors.white, fontWeight: FontWeight.bold),
46+
)
47+
],
48+
),
49+
),
50+
),
51+
),
52+
),
53+
],
54+
),
55+
),
56+
);
57+
}
58+
}

‎lib/model.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
2+
3+
class ItemModel {
4+
final String id, description, name, userId;
5+
final bool active, deleted, featured;
6+
7+
ItemModel(
8+
{this.name,
9+
this.active,
10+
this.deleted,
11+
this.description,
12+
this.featured,
13+
this.id,
14+
this.userId});
15+
16+
factory ItemModel.fromFirestore(DocumentSnapshot doc) {
17+
Map data = doc.data();
18+
return ItemModel(
19+
id: doc.id,
20+
name: data["name"] ?? null,
21+
active: data["active"] ?? false,
22+
deleted: data["deleted"] ?? false,
23+
description: data["description"] ?? null,
24+
featured: data["featured"] ?? false,
25+
userId: data['userId'] ?? null,
26+
);
27+
}
28+
Map<String, dynamic> toMap() {
29+
return {
30+
"name": name,
31+
"active": active,
32+
"description": description,
33+
"featured": featured,
34+
};
35+
}
36+
}

‎lib/sample.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:flutter/material.dart';
2+
3+
class Sample extends StatefulWidget {
4+
Sample({Key key}) : super(key: key);
5+
6+
@override
7+
_SampleState createState() => _SampleState();
8+
}
9+
10+
class _SampleState extends State<Sample> {
11+
@override
12+
Widget build(BuildContext context) {
13+
return Container();
14+
}
15+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /