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

149

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)));
Share:
149
Younghak Jang
Author by

Younghak Jang

Updated on January 03, 2023

Comments

  • Younghak Jang
    Younghak Jang over 1 year

    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?