Infernale

Firebase Dart

Aug 26th, 2020 (edited)
1,537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.15 KB | None | 0 0
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2. import 'package:firebase_auth/firebase_auth.dart';
  3. import 'package:test_project/model/todo.dart';
  4. import 'package:test_project/firebase/sign_in.dart';
  5.  
  6. final FirebaseFirestore database = FirebaseFirestore.instance;
  7. final FirebaseAuth auth = FirebaseAuth.instance;
  8.  
  9. var todoSize = 1;
  10. var todoItemSize = 1;
  11. List<Todo> firebaseTodos = [];
  12.  
  13. void countTodos() {
  14.   var docs = database.collection('users').doc(currentLoggedIn.email).get();
  15.   docs.then((snapshot) {
  16.     todoSize = snapshot.data()['todo_count'];
  17.   });
  18. }
  19.  
  20. void countTodoItem(int index) {
  21.   var docs = database.collection('users').doc(currentLoggedIn.email)
  22.                      .collection('todo').doc('todo$index').get();
  23.   docs.then((snapshot) => todoItemSize = snapshot.data()['items']);
  24. }
  25.  
  26. Future<void> loadTodoFromFirebase() async {
  27.   var docs = database.collection('users').doc(currentLoggedIn.email)
  28.                       .collection('todo').get();
  29.   docs.then((QuerySnapshot snapshot) {
  30.     snapshot.docs.forEach((element) {
  31.       firebaseTodos.add(Todo(element.data()['title'], element.data()['description']));
  32.     });
  33.   });
  34. }
  35.  
  36. void updateTodoCount(int amount) {
  37.   countTodos();
  38.   database.collection('users').doc(currentLoggedIn.email).update({
  39.     'todo_count': todoSize + 1
  40.   });
  41. }
  42.  
  43. void saveTodo(String title, String description) async {
  44.   database.collection('users').doc(currentLoggedIn.email)
  45.           .collection('todo').doc('todo$todoSize').set({
  46.             'title': title,
  47.             'description': description,
  48.           });
  49.   database.collection('users').doc(currentLoggedIn.email)
  50.         .collection('todo').doc('todo$todoSize')
  51.         .collection('item').doc('item0').set({
  52.           'item_name': 'Example Item',
  53.           'done': false
  54.         });
  55.   updateTodoCount(1);
  56. }
  57.  
  58. void saveTodoItem(int index, String name) async {
  59.   print("index");
  60.   print(index);
  61.   database.collection('users').doc(currentLoggedIn.email)
  62.           .collection('todo').doc('todo$index')
  63.           .collection('item').doc('item$todoItemSize').set({
  64.             'item_name': name,
  65.             'done': false,
  66.           });
  67. }
Add Comment
Please, Sign In to add comment