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
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.
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.