import 'dart:async'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:fcs/data/services/services.dart'; import 'package:fcs/domain/constants.dart'; import 'package:fcs/domain/entities/fcs_shipment.dart'; import 'package:fcs/pages/main/model/base_model.dart'; import 'package:logging/logging.dart'; class FcsShipmentModel extends BaseModel { final log = Logger('FcsShipmentModel'); StreamSubscription listener; List fcsShipments = []; @override void privilegeChanged() { super.privilegeChanged(); _loadFcsShipments(); } Future _loadFcsShipments() async { if (user == null) return; String path = "/$fcs_shipment_collection/"; if (listener != null) listener.cancel(); fcsShipments = []; try { listener = Firestore.instance .collection("$path") .orderBy("shipment_number", descending: true) .snapshots() .listen((QuerySnapshot snapshot) { fcsShipments.clear(); fcsShipments = snapshot.documents.map((documentSnapshot) { var s = FcsShipment.fromMap( documentSnapshot.data, documentSnapshot.documentID); return s; }).toList(); notifyListeners(); }); } catch (e) { log.warning("Error!! $e"); } } void initUser(user) { super.initUser(user); } @override logout() async { fcsShipments = []; } Future create(FcsShipment fcsShipment) { return Services.instance.fcsShipmentService.createFcsShipment(fcsShipment); } Future update(FcsShipment fcsShipment) { return Services.instance.fcsShipmentService.updateFcsShipment(fcsShipment); } }