[flutter] cloud_firestore

from flutter 2018. 4. 5. 11:37

선행으로 firebase google login 부분이 진행 되어야 합니다.



Cloud firebase setting


Database 규칙에  이래와 같이 넣어줍니다.


service cloud.firestore {
match /databases/{database}/documents {
match /messages/{anything=**}{
  allow read,write: if true;
}
match /{document=**} {
allow read, write: if false;
}
}
}



데이터의 컬렉션에 messages 메시지 추가




pubspec.yaml


dependencies:
flutter:
sdk: flutter

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cloud_firestore: "^0.2.10"
cupertino_icons: ^0.1.0
google_sign_in: "^3.0.0"
firebase_auth: "^0.5.3"

이상하게 cloud_firestore  0.4.0 버전을 사용하면 패키지를 못가지고오고  그 아래 버전 몇개는 패키지는 가지고오는데 컴파일이 안되더군요.. 이부분 해결하신분 좀 알려주세요  




다음은  cloud_firestore 샘플 코드를 넣어줍니다.


import 'dart:async';

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

void main() {
runApp(new MaterialApp(title: 'Firestore Example', home: new MyHomePage()));
}

class MessageList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('messages').snapshots,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return const Text('Loading...');
final int messageCount = snapshot.data.documents.length;
return new ListView.builder(
itemCount: messageCount,
itemBuilder: (_, int index) {
final DocumentSnapshot document = snapshot.data.documents[index];
return new ListTile(
title: new Text(document['message'] ?? '<No message retrieved>'),
subtitle: new Text('Message ${index + 1} of $messageCount'),
);
},
);
},
);
}
}

class MyHomePage extends StatelessWidget {
CollectionReference get messages => Firestore.instance.collection('messages');

Future<Null> _addMessage() async {
messages.document().setData(<String, String>{'message': 'Hello world!'});
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text('Firestore Example'),
),
body: new MessageList(),
floatingActionButton: new FloatingActionButton(
onPressed: _addMessage,
tooltip: 'Increment',
child: new Icon(Icons.add),
),
);
}
}













,