尽管我首先将其作为地图上传,但从 Firebase RealtimeDatabase 获取数据作为地图时遇到了一些问题。我看到的所有解决方案都是您应该将 snapshot.value 转换为传入的数据类型,但对我来说没有任何迭代。我已经可以打电话了toString()
在快照值上,我已经验证它是一个 Map,但编译器甚至不允许我将它转换为 Map,更不用说运行它了。如果我尝试使用动态来防止编译错误,我仍然会得到这个 Unhandled Exception: type '_InternalLinkedHashMap<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>?' in type cast
.
这就是我所做的,
Future<Map<String, dynamic>> getBasicDetail(
DatabaseReference databaseReference) async {
DatabaseReference imageURLevent = databaseReference.child('basicDetail');
final snapShot = await imageURLevent.get();
return Map<String, dynamic>.from(
snapShot.value as Map<String, dynamic>? ?? {});
}
Future<void> readProfileData({required String userId}) async {
DatabaseReference ref = FirebaseDatabase.instance.ref("users/$userId");
final basicDetail = await getBasicDetail(ref);
// Print the data of the snapshot
debugPrint(basicDetail.toString());
}
我已经尝试了解析这些数据的所有可能的组合,但没有任何效果。由于某些原因,在示例和视频中,当您将右侧投射到Map<String,dynamic>
你没有得到任何错误,但我的 IDE 甚至不允许我这样做。我也尝试过 Map.from() 似乎是一种流行的方法,但我的简单方法会引发运行时错误。
Why does this from
我的同时工作
doesn't?
event.snapshot.value 看起来也有效
[log] {bloodGroup: gh, imageURL: /data/user/0/com.piyushlimited.buzzai/app_flutter/image_picker7287835118545461685.jpg, fullName: Henry Nana Adu, weight: 523, dateOfBirth: 11/11/11, licenseNumber: 256489, age: 28}
我可以通过引用数据的完整路径来访问数据的一个值,但我有一张大地图,我无法对每个类别中的每个值执行此操作。看起来既费钱又浪费时间。我花了几天时间尝试了很多事情,所以请记住这一点。