'flutter - errors event.snapshot.value since updating to firebase 9.0.X

I'm getting errors on event.snapshot.value since updating to firebase 9.0.5. I have many functions like this which worked fine in 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;
  }

Now I have error marks on event.snapshot.value, and android studio says

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)));

When I try

Map<String, dynamic>.from(event.snapshot.value as Map<String, dynamic>).forEach((key, value) => 

then the error marker is gone but when I run the app it returns

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: 

What exactly changed in firebase 9.0? How can I iterate through event.snapshot.value in firebase 9.0?



Solution 1:[1]

From Firebase v9, they moved from using dynamic to Object? and it can be quite an hassle to start converting Object? to Map as you have experienced..Simply making the the object dynamic would remove the hassle.

Try:

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

Solution 2:[2]

statut: json['statut'] != null ? (json['statut'] as List).map((i) => Statut.fromJson(Map<String, dynamic>.from(i as dynamic))).toList() : null,

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ndiaga GUEYE