Flutter Firestore CRUD
궁금한 점이나 오류는 댓글로 달아주시면, 답변 혹은 수정하겠습니다! “:)”
- Flutter with firestore for CRUD
1. Flutter viscose Setting
1) flutter login 2) flutter pub global activate flutter fire_cli 3) Flutter fire configure 1) select the project created at firebase console 2) select platform (android, ios, macos, web) 4) flutter pub add firebase_core 5) flutter pub add cloud_firestore
2. Flutter main.dart, firebase.initializeApp
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(const MyApp());
}
3 Flutter CRUD from firebase
import 'package:cloud_firestore/cloud_firestore.dart';
class FirestoreService {
// get collection of notes
final CollectionReference notes =
FirebaseFirestore.instance.collection('notes');
// CREATE : add a new note
Future<void> addNote(String note) {
return notes.add({
'note': note,
'timestamp': Timestamp.now(),
});
}
// READ : get notes from database
Stream<QuerySnapshot> getNotesStream() {
final notesStream =
notes.orderBy('timestamp', descending: true).snapshots();
return notesStream;
}
// UPDATE : update notes given a doc id
Future<void> updateNote(String docID, String newNote) {
return notes.doc(docID).update({
'note': newNote,
'timestamp': Timestamp.now(),
});
}
// DELETE : delete notes given a doc id
Future<void> deleteNote(String docID) {
return notes.doc(docID).delete();
}
}
Youtube - Mitch Koko : 🔥📱 Flutter x Firebase CRUD Masterclass • Create / Read / Update / Delete
댓글남기기