Home > Mobile >  How to write data to firebase like this format in flutter
How to write data to firebase like this format in flutter

Time:06-17

How to write data to firebase like this format?

enter image description here

the qHFij7jKyTa1t9RhkWN2LImqwVl2 is User ID, the -MjYl_8EVJJkWcHgn6mJ is post ID,

I tried to used this code

await _firestore
            .collection('Notifications')
            .doc(FirebaseAuth.instance.currentUser!.uid)
            .collection(postid)
            .doc()
            .set({
          'userid': FirebaseAuth.instance.currentUser!.uid,
          'comment': 'test',
          'postid': postid,
          'ispost': true,
          'time': DateTime.now(),
        });

enter image description here

But it was not I want it.

In my old project in Android, the code is

ref = ref.getInstance.getReference("Notifications").child(publisherid);
HashMap<String, Object> hashmap = new HashMap<>();
hashmap.put("userid", userid);
hashmap.put("comment", comment);
hashmap.put("postid", postid);
hashmap.put("ispost", true);
hashmap.put("time", DateTime.now());

How can do like this format?

enter image description here

CodePudding user response:

You can easily create sub-collection by following way.

CollectionReference notifications = FirebaseFirestore.instance.collection('Notifications');

notifications.doc('qHFij7jKyTa1t9RhkWN2LImqwVl2').collection('MjYl_8EVJJkWcHgn6mJ').add({
            'comment': 'liked your post',
            'isPost': 'true', 
          });

CodePudding user response:

FirebaseFirestore.instance.collection('Notifications')

.doc('qHFij7jKyTa1t9RhkWN2LImqwVl2').collection('MjYl_8EVJJkWcHgn6mJ')

.add({ 'comment': 'liked your post', 'isPost': 'true', });

  • Related