Flutter Firebase 書き方

参照

Eメール認証

ユーザー登録

1
2
3
4
5
6
7
8
9
10
11
12
13
14
FirebaseUser user; 
void _registerUser(String _email,String _password) async {
user = (await _auth.createUserWithEmailAndPassword(
email: _email,
password: _password,
)).user;
if (user != null) {
// User registered successfully
// Add user details in realtime database or cloudfirestore
} else {
// User not registered Successfully
}
}
}

ログイン

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
final FirebaseUser user  
void _signInWithEmailAndPassword(String _email,String _password) async {
user = (await _auth.signInWithEmailAndPassword(
email: _email,
password: _password,
)).user;
if (user != null) {
// Login successful
// Navigate to main page
} else {
// Invalid email and password
// Navigate to registration page
}
}
}

Firebase

データ保存

  • add は、新しいDocumentIdを生成して、新しいレコードとして保存
1
2
3
4
5
Future<void> newGroup() {
return Firestore.instance.collection("hoge").add({
"hoge": hogehoge
});
}
  • set は、ドキュメントの追加、上書き
  • 既にドキュメントが存在すると、ドキュメントごと全て上書き
  • mergeオプションをつければ、fieldだけの一部追加も可能
1
2
3
4
5
6
Future<void> editGroup() {
var db = Firestore.instance;
return db.collection("group").document(groupID).setData({
"hoge": hogehoge
});
}

データ更新

  • update は、ドキュメントのfieldの値を更新
  • ドキュメントがないとエラーになる
1
2
3
4
5
6
Future<void> _uploadImage(String documentId,) async {
return Firestore.instance
.collection("group")
.document(documentId)
.updateData({"documentId": documentId});
}

データ参照

  • データ取得
1
2
3
4
5
6
Stream<QuerySnapshot> getGroupItem(String groupId) {
return Firestore.instance
.collection("group")
.document(groupId)
.get();
}
1
2
3
4
5
6
7
8
Future<Group> getGroupInfo(String groupId) async {
var document = Firestore.instance.collection("group").document(groupId).get();
return await document.then((doc) {
return Group.setGroup(doc);
});
}
// 呼び出し
var groupInfo = await helper.getGroupInfo(widget.groupId);
  • Streamのsnapshotを取得
1
2
3
4
5
6
Stream<QuerySnapshot> getGroupItem(String groupId) {
return Firestore.instance
.collection("group")
.document(groupId)
.snapshots();
}
  • Streamのsnapshotを更新日付け順で取得
1
2
3
4
5
6
Stream<QuerySnapshot> getGroupItem(String groupId) {
return Firestore.instance
.collection("group")
.orderBy("updated_at", descending: true)
.snapshots();
}
  • Streamのsnapshotをタイトルが同じものだけ取得
1
2
3
4
5
6
Stream<QuerySnapshot> searchGroup(String title) {
return Firestore.instance
.collection("group")
.where("title", isEqualTo: title)
.snapshots();
}
  • Document一覧を取得
1
2
3
4
5
Stream<QuerySnapshot> getGroupItem(String groupId) {
return Firestore.instance
.collection("group")
.getDocuments();
}
  • Documentを取得
1
2
3
4
5
6
7
8
Future getData(String collection, String documentId) async {
DocumentSnapshot docSnapshot =
await Firestore.instance
.collection(collection)
.document(documentId)
.get();
return docSnapshot.data;
}

データ削除

1
2
3
4
5
6
Future<void> removeImage(String groupId) {
return Firestore.instance
.collection("group")
.document(groupId)
.delete();
}
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
 Future<void> removeImage(String groupId, String groupDetailId) {
return Firestore.instance
.collection("group")
.document(groupId)
.collection("group_item")
.document(groupDetailId)
.delete();
}
```

### データ有無チェック

```dart
Query colRef = Firestore.instance
.collection('user')
.where('mobile', isEqualTo:_phoneNumber)
.where('type',isEqualTo:'editor');

colRef.getDocuments().then((data){
if(data.documents.isNotEmpty){
data.documents.forEach((data){
print(data['Name']);
});
Toast.show("authorize User", context,
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
} else{
Toast.show("Unauthorize User", context,
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
}
});

collection と document の違い

データ構造

collection

  • documentの集まり
  • Collection.get()で、documentの集まりを取得
  • 返ってくるオブジェクトは、QuerySnapshot
  • データへのアクセスは、QuerySnapshot.documents[0].data()

document

  • dataの集まり
  • Document.get()で、dataの集まりを取得
  • 返ってくるオブジェクトは、DocumentSnapshot
  • データへのアクセスは、DocumentSnapshot.data()

コメント

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×