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

2023-11-26

我收到错误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;
  }

现在我有错误标记event.snapshot.value,and 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 开始,他们不再使用dynamic to Object?开始转换可能会很麻烦Object? to Map正如您所经历的......简单地制作对象dynamic会消除麻烦。

Try:

Map<String, dynamic>.from(event.snapshot.value as dynamic).forEach((key, value) => _mentorList.add(MentorModel.fromRTDB(key, value)));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

flutter - 自更新到 firebase 9.0.X 以来出现错误 event.snapshot.value 的相关文章

随机推荐