【问题标题】:Flutter Firebase Firestore Queries with Document Fieldpaths with maps带有文档字段路径和地图的 Flutter Firebase Firestore 查询
【发布时间】:2020-09-29 23:30:02
【问题描述】:

我正在尝试根据其他用户是否看到它来对文档列表进行排序。 我的数据库结构如下:

following (Document Field){
Dy2k9f2m7uXnnBBPHssPxJucCrK2 : true (Map)
HOIXdQkoerRBgYCGHFRylQD2VKi1 : true (Map)
}

我试过运行这个命令

print(
user.data()['following'].toString(),
);

并收到输出

{HOIXdQkoerRBgYCGHFRylQD2VKi1:真,Dy2k9f2m7uXnnBBPHssPxJucCrK2: 真的}

但我想知道指定的用户 id 说 HOIXdQkoerRBgYCGHFRylQD2VKi1 的值是 true 还是 false。

我该怎么做?非常感谢任何帮助。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    使用user.data()['following'] 访问的Firestore 地图类型字段返回Map<String, dynamic>,因此您可以像对待dart 中的任何其他地图一样对待它。如果您想假设它包含bool 值:

    Map<String, bool> following = user.data()['following'];
    bool seen = following['the-user-id-to-check'];
    

    【讨论】:

    • 输出显示类型'String'不是类型'Map'的子类型
    • 所以,字符串中的 S 大写。
    • 我猜这个问题又是因为数据类型是动态的
    【解决方案2】:

    正如 Doug 所建议的,这里的问题是该值可能是动态的。 您可以将其转换为 bool 变量:

    bool see = false;
    String userKey = "Dy2k9f2m7uXnnBBPHssPxJucCrK2"; // <-- User Key
    
    print(
      see = user.data()['following'][userKey] as bool, // <-- cast as bool
    );
    

    【讨论】:

      猜你喜欢
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多