Home > Software engineering >  flutter - errors event.snapshot.value since updating to firebase 9.0.X
flutter - errors event.snapshot.value since updating to firebase 9.0.X

Time:01-18

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?

CodePudding user response:

From Firebase v9, the 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) => 
  •  Tags:  
  • Related