Flutter Firebase Firestore查询与带地图的文档字段路径



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

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

我试着运行这个命令

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

并接收到输出

{HOIXdQkoerRBgYCGHFRylQD2VKi1:true,Dy2k9f2m7uXnnBBPHssPxJucCrK2:true}

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

我该怎么做?非常感谢您的帮助。

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

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

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

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

最新更新