Facebook
From Funky Armadillo, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 123
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2.  
  3. class ApiService{
  4.   final Firestore _db = Firestore.instance;
  5.   final String _path;
  6.   CollectionReference _ref;
  7.  
  8.   ApiService(this._path) {
  9.     _ref = _db.collection(_path);
  10.   }
  11.  
  12.   Future<QuerySnapshot> getDataCollection() {
  13.     return _ref.getDocuments();
  14.   }
  15.  
  16.   Stream<QuerySnapshot> streamDataCollectionContains(
  17.       String field, dynamic value) {
  18.     return _ref
  19.         .orderBy(field, descending: false).startAt([value]).endAt([value+ '\uf8ff'])
  20.         .snapshots();
  21.   }
  22.  
  23.   Stream<QuerySnapshot> streamDataCollectionWhere(
  24.       String field, dynamic value) {
  25.     return _ref
  26.         .where(field, isEqualTo: value)
  27.         .snapshots();
  28.   }
  29.  
  30.   Stream<QuerySnapshot> streamDataCollection() {
  31.     return _ref.snapshots();
  32.   }
  33.  
  34.   Stream<DocumentSnapshot> streamDataCollectionById(String id) {
  35.     return _ref.document(id).snapshots();
  36.   }
  37.  
  38.   Future<DocumentSnapshot> getDocumentById(String id) {
  39.     return _ref.document(id).get();
  40.   }
  41.  
  42.   Future<DocumentReference> create(Map data) {
  43.     return _ref.add(data);
  44.   }
  45.  
  46.   Future<void> delete(String id) {
  47.     return _ref.document(id).delete();
  48.   }
  49.  
  50.   Future<void> update(Map data, String id) {
  51.     return _ref.document(id).updateData(data);
  52.   }
  53. }