简体   繁体   English

如何在颤动中像这种格式一样将数据写入firebase

[英]How to write data to firebase like this format in flutter

How to write data to firebase like this format?如何像这种格式将数据写入firebase?

在此处输入图像描述

the qHFij7jKyTa1t9RhkWN2LImqwVl2 is User ID, the -MjYl_8EVJJkWcHgn6mJ is post ID, qHFij7jKyTa1t9RhkWN2LImqwVl2是用户 ID, -MjYl_8EVJJkWcHgn6mJ是帖子 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在我在 Android 的旧项目中,代码是

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') FirebaseFirestore.instance.collection('通知')

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

.add({ 'comment': 'liked your post', 'isPost': 'true', }); .add({ 'comment': '喜欢你的帖子', 'isPost': 'true', });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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