简体   繁体   中英

How to write data to firebase like this format in flutter

How to write data to firebase like this format?

在此处输入图像描述

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(),
        });

在此处输入图像描述

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?

在此处输入图像描述

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', 
          });

FirebaseFirestore.instance.collection('Notifications')

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM