You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Forms中使用Plugin.CloudFirestore获取Firestore引用字段的类型问题

Firestore引用字段的正确映射类型(Plugin.CloudFirestore)

在Xamarin.Forms项目中使用Plugin.CloudFirestore时,Firestore的reference类型字段需要根据实际存储结构选择对应类型:

1. 单个文档引用字段

如果field3是单个文档引用,直接声明为IDocumentReference即可,不需要包裹IEnumerable<T>:

public class Fields
{
    // 建议修复string字段映射:更新Plugin.CloudFirestore到最新版后,直接用string类型
    public string field1 { get; set; }
    public string field2 { get; set; }
   
    // 单个引用字段的正确类型
    public IDocumentReference field3 { get; set; }
}

2. 文档引用数组字段

如果field3是多个文档引用组成的数组,才需要使用IEnumerable<IDocumentReference>:

public class Fields
{
    public string field1 { get; set; }
    public string field2 { get; set; }
   
    // 引用数组的正确类型
    public IEnumerable<IDocumentReference> field3 { get; set; }
}

取值示例

var fields = doc.ToObject<Fields>();
var thirdField = fields.field3;

// 获取单个引用文档的ID或路径
if (thirdField is IDocumentReference docRef)
{
    string docId = docRef.Id;
    string docPath = docRef.Path;
}

关于string字段的额外建议

你之前用IEnumerable<char>映射Firestore的string字段属于临时 workaround,通常是旧版本Plugin.CloudFirestore存在序列化兼容问题导致的。建议将库更新到最新稳定版,这样就能直接用string类型正常映射字符串字段,无需额外类型转换。

内容的提问来源于stack exchange,提问作者Costas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 10:25:13