【发布时间】:2022-05-04 09:24:12
【问题描述】:
自从更新到 firebase 9.0.5 后,我在 event.snapshot.value 上遇到了错误。
我有很多这样的功能,在 firebase 8.X 中运行良好。
Stream<List<MentorModel>> mentorStream() {
final stream = _database.onValue;
final Stream<List<MentorModel>> resultStream = stream.map((event) {
List<MentorModel> _mentorList = [];
Map<String, dynamic>.from(event.snapshot.value).forEach((key, value) => _mentorList.add(MentorModel.fromRTDB(key, value)));
return _mentorList;
});
return resultStream;
}
现在我在event.snapshot.value 上有错误标记,android studio 说
Error: The argument type 'Object?' can't be assigned to the parameter type 'Map<dynamic, dynamic>'.
- 'Object' is from 'dart:core'.
- 'Map' is from 'dart:core'.
Map<String, dynamic>.from(event.snapshot.value).forEach((key, value) => _mentorList.add(MentorModel.fromRTDB(key, value)));
当我尝试时
Map<String, dynamic>.from(event.snapshot.value as Map<String, dynamic>).forEach((key, value) =>
然后错误标记消失了,但是当我运行应用程序时它返回
E/flutter (16737): [ERROR:flutter/shell/common/shell.cc(93)] Dart Unhandled Exception: type '_InternalLinkedHashMap<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>' in type cast, stack trace:
firebase 9.0 到底发生了什么变化?如何在 firebase 9.0 中遍历 event.snapshot.value?
【问题讨论】:
-
Firebase v9 正常运行,v8 和下面是 OOP。
标签: firebase flutter firebase-realtime-database