skip to Main Content

I encoded a Firestore DocumentReference inside a json object. Then I embed this string in a QR code. Then read the QR code with "mobile_scanner" package

"subscriber_document_reference" : "${subscriber.subscriberDocumentReference}"

Once I decode it, I get map with String key and also a String value. I want to change this String to an actual DocumentReference I can use.

naturally a direct cast fails with the exception:

 "type 'String' is not a subtype of type 'DocumentReference<Object?>' in type cast"


Edit: This is the encoding

 final data = ' {"subscriber_document_reference": "${subscriber.subscriberDocumentReference}" }';

final payload = json.encode(data);

The subscriber.subscriberDocumentReference is the correct type here: DocumentReference<Map<String, dynamic>>

I decode it

final String? displayValue = barcodes.barcodes.first.displayValue;  // mobile_scanner package code
final temp = json.decode(displayValue) as String;
final payload = json.decode(temp) as Map<String, dynamic>;

the value now is

"subscriber_document_reference": "DocumentReference<Map<String, dynamic>>(......)

note: ignore the decoding twice. I don’t know why it’s doing that, but since it works, it works.

2

Answers


  1. Chosen as BEST ANSWER

    I found the answer with the help of @pskink. Instead of encoding the entire DocumentReference, I encoded the Document ID's that make up the DocumentReference. This works for me because the collection ID's are known and hardcoded.

    Then I can reconstruct the DocumentReference with the usual Firestore syntax.

    Firestore.instance.collection("my_collection_id").doc("decoded_id").collection("another_collection_id").doc("another_decoded_id")
    

  2. Not sure what you are trying to do, but
    FirebaseFirestore.instance.collection(‘nameOfYourCollection’).doc(‘yourString’)
    should give a DocumentReference.

    Or your error could be due to your DocumentReference is not converted to String, so where ever you decode, use toString() function.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search