我有一个包含数据的映射。但当我执行这个map["content"]时,它告诉我null。有什么问题吗?
This is the code
final messages = snap.data!.snapshot.value as Map<dynamic, dynamic>;
This is what I'm doing
messages["idFrom"]
消息是Map<dynamic, dynamic>
或Map<int, dynamic>
从您的响应
Map<int, dynamic> messages = {
1673561668839422: {
'timeStamp': 1673561668840253,
'idTo': 'hMKNXQxOHTSi6WqbuHN5uabQwea2',
'idFrom': '7BEeF0rHihfIFRvv9ClqIdAzzPP2',
'type': 0,
'content': 'Hi'
},
1673562395023337: {
'timeStamp': 1673562395023396,
'idTo': 'hMKNXQxOHTSi6WqbuHN5uabQwea2',
'idFrom': '7BEeF0rHihfIFRvv9ClqIdAzzPP2',
'type': 0,
'content': 'How are you'
},
...
};
不能执行messages['idFrom']
,因为idFrom
是嵌套Map
的键,即
{
'timeStamp': 1673561668840253,
'idTo': 'hMKNXQxOHTSi6WqbuHN5uabQwea2',
'idFrom': '7BEeF0rHihfIFRvv9ClqIdAzzPP2',
'type': 0,
'content': 'Hi'
}
如果你想访问idFrom
,你可以访问messages[int]['idFrom']
,其中int
是Map
消息的密钥之一。
交货. .print(messages[1673561668839422]['idFrom'])