【发布时间】:2021-11-16 22:30:03
【问题描述】:
(这个孩子)
我尝试使用此代码,但它在访问 snapshot.value 时返回 {-MkHrcId8EfYpqFWqNrw: {Sender: 97205********, message: hello}},但在尝试访问 snapshot.value['message'] 时返回 null
Future<String> getLastMessage() async{
final ref = FirebaseDatabase.instance.reference();
String RoomId; // RoomId is the name of the first child, the one that starts with 587 in the image
String lastMessage;
lastMessage = await ref.child(RoomId).orderByKey().limitToLast(1).once().then((snapshot) {
snapshot.value['message'];
return lastMessage;
});
【问题讨论】:
-
代码乍一看还不错。您可以在
then()回调中print(snapshot.value)并编辑您的问题以显示更新的代码及其输出吗? -
代码在打印
snapshot.value时实际上返回{-MkHrcId8EfYpqFWqNrw: {Sender: 97205********, message: hello}},但在尝试打印snapshot.value['message']时返回null@Frank van Puffelen
标签: flutter dart firebase-realtime-database