Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:cloud_firestore/cloud_firestore.dart';
- import 'package:firebase_auth/firebase_auth.dart';
- import 'package:test_project/model/todo.dart';
- import 'package:test_project/firebase/sign_in.dart';
- final FirebaseFirestore database = FirebaseFirestore.instance;
- final FirebaseAuth auth = FirebaseAuth.instance;
- var todoSize = 1;
- var todoItemSize = 1;
- List<Todo> firebaseTodos = [];
- void countTodos() {
- var docs = database.collection('users').doc(currentLoggedIn.email).get();
- docs.then((snapshot) {
- todoSize = snapshot.data()['todo_count'];
- });
- }
- void countTodoItem(int index) {
- var docs = database.collection('users').doc(currentLoggedIn.email)
- .collection('todo').doc('todo$index').get();
- docs.then((snapshot) => todoItemSize = snapshot.data()['items']);
- }
- Future<void> loadTodoFromFirebase() async {
- var docs = database.collection('users').doc(currentLoggedIn.email)
- .collection('todo').get();
- docs.then((QuerySnapshot snapshot) {
- snapshot.docs.forEach((element) {
- firebaseTodos.add(Todo(element.data()['title'], element.data()['description']));
- });
- });
- }
- void updateTodoCount(int amount) {
- countTodos();
- database.collection('users').doc(currentLoggedIn.email).update({
- 'todo_count': todoSize + 1
- });
- }
- void saveTodo(String title, String description) async {
- database.collection('users').doc(currentLoggedIn.email)
- .collection('todo').doc('todo$todoSize').set({
- 'title': title,
- 'description': description,
- });
- database.collection('users').doc(currentLoggedIn.email)
- .collection('todo').doc('todo$todoSize')
- .collection('item').doc('item0').set({
- 'item_name': 'Example Item',
- 'done': false
- });
- updateTodoCount(1);
- }
- void saveTodoItem(int index, String name) async {
- print("index");
- print(index);
- database.collection('users').doc(currentLoggedIn.email)
- .collection('todo').doc('todo$index')
- .collection('item').doc('item$todoItemSize').set({
- 'item_name': name,
- 'done': false,
- });
- }
Add Comment
Please, Sign In to add comment