Document<T> constructor

Document<T>({
  1. String? id,
  2. String? documentPath,
  3. String? collectionPath,
  4. DocumentSnapshot<Map<String, dynamic>>? snapshot,
  5. Map<String, dynamic>? values,
  6. CollectionReference<Map<String, dynamic>>? collectionRef,
})

Implementation

Document({
  String? id,
  String? documentPath,
  String? collectionPath,
  this.snapshot,
  this.values,
  CollectionReference<Map<String, dynamic>>? collectionRef,
})  : assert(
        id == null || documentPath == null,
        'Can be used only either of \'id\' or \'documentPath\'.',
      ),
      // ignore: lines_longer_than_80_chars
      assert(
        documentPath == null || collectionPath == null,
        // ignore: lines_longer_than_80_chars
        'Can be used only either of \'documentPath\' or \'collectionPath\'.',
      ),
      assert(
        collectionPath == null || collectionRef == null,
        // ignore: lines_longer_than_80_chars
        'Can be used only either of \'collectionPath\' or \'collectionRef\'.',
      ) {
  if (documentPath != null) {
    /// From reference path.
    final referenceDocument = Flamingo.instance.firestore.doc(documentPath);
    _id = referenceDocument.id;
    _collectionRef = referenceDocument.parent;
    _reference = referenceDocument;
  } else {
    /// Set collectionRef
    if (collectionPath != null) {
      _collectionRef = Flamingo.instance.firestore.collection(collectionPath);
    } else if (collectionRef != null) {
      _collectionRef = collectionRef;
    } else if (snapshot != null) {
      _collectionRef = snapshot!.reference.parent;
    } else {
      _collectionRef = collectionRootReference;
    }

    /// Set reference and Id
    if (id != null) {
      _reference = _collectionRef.doc(id);
      _id = id;
    } else {
      _reference = _collectionRef.doc();
      _id = _reference.id;
    }

    /// From snapshot
    if (snapshot != null) {
      setSnapshot(snapshot!); // setSnapshotでidが作られる
      _reference = _collectionRef.doc(_id);
    }
  }

  /// From values
  if (values != null) {
    _fromAt(values!);
    fromData(values!);
  }

  /// Set path
  _collectionPath = _collectionRef.path;
  _documentPath = _reference.path;
}