Skip to content

Instantly share code, notes, and snippets.

@felangel
Created August 4, 2019 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felangel/73c27c3df1f2a53e74bd2ac8009f755f to your computer and use it in GitHub Desktop.
Save felangel/73c27c3df1f2a53e74bd2ac8009f755f to your computer and use it in GitHub Desktop.
[flutter_firestore_todos] firebase todos repository
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:todos_repository/todos_repository.dart';
import 'entities/entities.dart';
class FirebaseTodosRepository implements TodosRepository {
final todoCollection = Firestore.instance.collection('todos');
@override
Future<void> addNewTodo(Todo todo) {
return todoCollection.add(todo.toEntity().toDocument());
}
@override
Future<void> deleteTodo(Todo todo) async {
return todoCollection.document(todo.id).delete();
}
@override
Stream<List<Todo>> todos() {
return todoCollection.snapshots().map((snapshot) {
return snapshot.documents
.map((doc) => Todo.fromEntity(TodoEntity.fromSnapshot(doc)))
.toList();
});
}
@override
Future<void> updateTodo(Todo update) {
return todoCollection
.document(update.id)
.updateData(update.toEntity().toDocument());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment