Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Firestore: Write document reference from Flutter Plugin/SDK

I'm looking to write a document reference from a Flutter app to Firestore.

This is how it looks if I create a reference from the Firestore Console: enter image description here

If I just write the references inside a String from Flutter

  Map<String, String> usersReference = {
    uid: 'users/' + uid
  };
  Map<String, Object> userData = {
    'usersReference': usersReference
  };
  Firestore.instance.collection('chats').add(userData).then((doc) {
    doc.setData(userData);
  });

I get this:

enter image description here

Is there a way to write a reference (like from the Console) using the Flutter cloud_firestore plugin or any other SDK?

like image 758
tobire Avatar asked Apr 27 '26 00:04

tobire


1 Answers

You should not declare an Object for the reference change it to DocumentReference

Map<String, DocumentReference> userData = {
  'usersReference': usersReference
};
Firestore.instance.collection('chats').add(userData).then((doc) {
  doc.setData(userData);
});
like image 71
JLouage Avatar answered Apr 29 '26 20:04

JLouage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!