Flutter Cloud Firestore ODM, how to convert DocumentReference<Model> to DocumentReference<Map<String, dynamic>>

huangapple go评论58阅读模式
英文:

Flutter Cloud Firestore ODM, how to convert DocumentReference<Model> to DocumentReference<Map<String, dynamic>>

问题

To convert a VendorDocumentReference to a DocumentReference<Map<String, dynamic>>, you can use the DocumentReference constructor. Here's how you can achieve this conversion:

// Assuming you have a VendorDocumentReference named vendorDocRef
DocumentReference<Map<String, dynamic>> convertedRef = DocumentReference<Map<String, dynamic>>(
  vendorDocRef.firestore,
  vendorDocRef.path,
);

// Now, convertedRef is a DocumentReference<Map<String, dynamic>> that you can use in your Invoice creation.

This code creates a new DocumentReference using the Firestore instance and path of your VendorDocumentReference, which effectively converts it into the desired type.

英文:

If I have two collections in Firebase:

Invoices:
    invoice1 {
        Vendor: (reference) /Vendors/vendor1
    }

Vendors:
    vendor1 {
        Name: &quot;John Doe&quot;
}

and if I model them with cloud firestore odm generator as:

@Collection&lt;Invoice&gt;(&#39;Invoices&#39;)
@firestoreSerializable
class Invoice {
  Invoice({
      required this.id,
      required this.vendor,
  });

    @Id()
    String id;
    @JsonKey(name: &#39;Vendor&#39;)
    DocumentReference&lt;Map&lt;String, dynamic&gt;&gt; vendor;
}

final invoicesRef = InvoiceCollectionReference();


@Collection&lt;Vendor&gt;(&#39;Vendors&#39;)
@firestoreSerializable
class Vendor {
    Vendor({
      required this.id,
      required this.name,
  });

    @Id()
    String id;
    @JsonKey(name: &#39;Name&#39;)
    String name;
}

final vendorsRef = VendorCollectionReference();

when I select a vendor from vendorsRef, I obtain a VendorDocumentReference. In order to create a new invoice with this vendor, I need to convert it to DocumentReference<Map<String, dynamic>>. How do I achieve this conversion?

VendorDocumentReference or DocumentReference<Vendor> don't seem to have a way to perform this conversion.

答案1

得分: 0

不要紧。我已经用转换器解决了:

    class VendorConverter extends JsonConverter<DocumentReference<Vendor>,
        DocumentReference<Map<String, dynamic>>> {
      @

<details>
<summary>英文:</summary>

Nevermind. I&#39;ve solved with converter:

    class VendorConverter extends JsonConverter&lt;DocumentReference&lt;Vendor&gt;,
        DocumentReference&lt;Map&lt;String, dynamic&gt;&gt;&gt; {
      @override
      DocumentReference&lt;Vendor&gt; fromJson(
          DocumentReference&lt;Map&lt;String, dynamic&gt;&gt; json) {
            return vendorsRef.doc(json.id).reference;
      }

      @override
      DocumentReference&lt;Map&lt;String, dynamic&gt;&gt; toJson(
          DocumentReference&lt;Vendor&gt; object) {
        final path = vendorsRef.doc(object.id).path;
        return FirebaseFirestore.instance.doc(path);
      }

      const VendorConverter();
    }

</details>



huangapple
  • 本文由 发表于 2023年6月15日 16:29:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定