flutter - 自更新到 firebase 9.0.X 以来出现错误 event.snapshot.value

You*_*ang 5 firebase firebase-realtime-database flutter

event.snapshot.value自从更新到 firebase 9.0.5 以来,我遇到了错误。我有很多这样的函数,它们在 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;
  }
Run Code Online (Sandbox Code Playgroud)

现在我有错误标记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)));
Run Code Online (Sandbox Code Playgroud)

当我尝试时

Map<String, dynamic>.from(event.snapshot.value as Map<String, dynamic>).forEach((key, value) => 
Run Code Online (Sandbox Code Playgroud)

然后错误标记消失了,但是当我运行应用程序时它会返回

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: 
Run Code Online (Sandbox Code Playgroud)

Firebase 9.0 到底发生了什么变化?如何在 firebase 9.0 中迭代 event.snapshot.value ?

Jos*_*eve 7

从 Firebase v9 开始,它们从使用转变dynamic为,正如您所经历的那样,开始转换Object?可能会非常麻烦。只需制作对象即可消除麻烦。Object?Mapdynamic

尝试:

Map<String, dynamic>.from(event.snapshot.value as dynamic).forEach((key, value) => _mentorList.add(MentorModel.fromRTDB(key, value)));
Run Code Online (Sandbox Code Playgroud)