【问题标题】:How update a reference type in Firestore with Flutter如何使用 Flutter 更新 Firestore 中的引用类型
【发布时间】:2018-10-01 01:46:18
【问题描述】:

我有一个包含 2 个字段的文档,一个整数和一个引用类型

主要 价值:10 参考:产品/9ci2hwiMUp63Q9Eugcqg

我想将字段 ref 更改为 products/guEgu2sjsyOsSEChu2fT。有一个类DocumentReference,唯一要更改的属性是路径,但它是只读属性。

我阅读了 firebase.google.com 中的文档,但没有关于如何更新这些类型的示例。

可以更新这种类型的字段吗?

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    您可以在文档引用上使用 UpdateData() 来更新数据:

    DocumentReference ref = Firestore.instance.collection('products').document('guEgu2sjsyOsSEChu2fT');
    Map<String,dynamic> data = {
            'main value' : 10,
             'ref' : ref,  // Updating Document Reference 
            };
    
            Firestore.instance.collection('Your Collection Name').document('your document name').updateData(data).whenComplete((){
              print('Document Updated');
            });
    

    【讨论】:

    • 只有在类型为字符串时才有效。我得到了 I/flutter(4535):引发了另一个异常:类型“String”不是“DocumentReference”类型的子类型
    • 支持的类型 - firebase.google.com/docs/firestore/manage-data/data-types 检查引用类型。
    • 这是真正的参考路径元素(集合、文档 ID、集合、文档 ID...),但我的问题与如何更新这种“数据类型”有关
    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 2021-01-18
    • 1970-01-01
    相关资源
    最近更新 更多