Skip to content

Commit

Permalink
feat: added getter for documents
Browse files Browse the repository at this point in the history
  • Loading branch information
PaytonWebber committed Jul 14, 2024
1 parent fa540a5 commit b0e6f90
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions lib/state/firebase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,28 @@ import '../models/models.dart';

class FirebaseState extends ChangeNotifier {
DatabaseReference ref = FirebaseDatabase.instance.ref();
final Document curDocument =
Document(id: '0', title: 'title', content: 'content');
var _documents = <Document>[];

List<Document> get documents => _documents;

AppState() {
fetchDocuments();
}

void fetchDocuments() {
ref.child('documents').onValue.listen((DatabaseEvent event) {
final data = event.snapshot.value as Map<dynamic, dynamic>;
_documents = data.entries.map((entry) {
var documentData = entry.value as Map<dynamic, dynamic>;
return Document(
id: entry.key,
title: documentData['title'],
content: documentData['content'],
);
}).toList();
notifyListeners();
});
}

void createUser(String name, List<String> documents) {
ref.child('users').push().set({
Expand All @@ -30,20 +50,4 @@ class FirebaseState extends ChangeNotifier {
});
}

void detectChange(String id) {
DatabaseReference curDocument =
FirebaseDatabase.instance.ref('documents').child(id);
curDocument.onValue.listen((DatabaseEvent event) {
final data = event.snapshot.value;
updateCurDocument(data);
});
}

void updateCurDocument(var data) {
//TODO: parse response data
curDocument.id = data['id'];
curDocument.title = data['title'];
curDocument.content = data['content'];
notifyListeners();
}
}

0 comments on commit b0e6f90

Please sign in to comment.