如何在Flutter中设置,获取和更新数据到Cloud Firestore? [英] How to set, get and update data to the cloud firestore in flutter?

查看:50
本文介绍了如何在Flutter中设置,获取和更新数据到Cloud Firestore?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一些代码,但是遇到了异常.

I tried some code but getting an exception.

我得到的异常: java.lang.IllegalArgumentException:无效的文档引用.文档引用必须具有偶数个段,但是用户具有1

我根据

I searched for it, according to this, Document references must have an even number of segments like: Collection - document - Collection - document - Collection - document

查询从Firestore获取数据

String getIsNewUSer;
Firestore.instance.collection('Users').document(uid).get().then((DocumentSnapshot document){
       print("document_build:$document");
        setState(() {
         getIsNewUSer=document['IsNewUser'];
         print("getIsNewUSe:$getIsNewUSer");
        });

    });

查询将数据更新到Firestore:

Firestore.instance
            .collection('Users')
            .document(uid)
            .updateData({
              "IsNewUser":"1"
            }).then((result){
              print("new USer true");
            }).catchError((onError){
             print("onError");
            });

这些代码行超出了Exception.

These code line at I'm getting above Exception.

initState:

 void initState()  {
    super.initState();
    this.uid = '';

FirebaseAuth.instance.currentUser().then((val){
      setState(() {
      this.uid= val.uid; 
       print("uid_init: $uid");
      });
   });

}

我做错了吗????? ..

Am I doing wrong?????..

推荐答案

在查询中替换此部分:

Firestore.instance.collection('Users').document(uid)

使用

Firestore.instance.document('Users/$uid')

收藏-文档-收藏-文档-收藏-文档

Collection - document - Collection - document - Collection - document

基本上您已经有了答案.

Basically you already had the answer.

这篇关于如何在Flutter中设置,获取和更新数据到Cloud Firestore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆