database.dart 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2. import 'models/user.dart';
  3. class DatabaseManager {
  4. CollectionReference usersList =
  5. FirebaseFirestore.instance.collection("Users");
  6. // Get stream with users ordered by total points from firestore
  7. Stream<List> getUsersList() async* {
  8. List<dynamic> users = [];
  9. try {
  10. await usersList.orderBy("total_points", descending: true).snapshots().
  11. listen((snap){
  12. snap.docs.forEach((element){
  13. users.add(element.data());
  14. });
  15. });
  16. yield users;
  17. } catch(e){
  18. print(e.toString());
  19. yield [];
  20. }
  21. }
  22. //It is not necessary.At least right now
  23. // List<User> _userListFromSnapshot(QuerySnapshot snapshot){
  24. // return snapshot.docs.map((doc){
  25. // return User(
  26. // shared_id: doc.get('shared_id'),
  27. // name: doc.get('name'),
  28. // total_points: doc.get('total_ponts'),
  29. // );
  30. // }).toList();
  31. // }
  32. //
  33. // dynamic get users {
  34. // return usersList.orderBy("total_points", descending: true).snapshots();
  35. // }
  36. }