Flutter - The argument type 'Object' can't be assigned to the parameter type 'Map<String, dynamic>'

Viewed 559

So I've been working on this project, and everything worked fine, until, I migrated the packages to null safety and encountered this error. Here's the code:

  factory AdminDto.fromJson(Map<String, dynamic> json) => _$AdminDtoFromJson(json);

  factory AdminDto.fromFirestore(DocumentSnapshot doc) {
    return AdminDto.fromJson(doc.data()!).copyWith(id: doc.id);
  }
}

Can someone please help me with this? The problem occurs when it comes to doc.data() parameter. I can't quite put my head into it.

1 Answers

You probably need to explicitly cast doc.data()! to Map<String, dynamic>. Like this:

return AdminDto.fromJson(doc.data()! as Map<String, dynamic>).copyWith(id: doc.id);

Or correctly define the generic type of DocumentSnapshot, like this:

factory AdminDto.fromFirestore(DocumentSnapshot<Map<String, dynamic>> doc) {
    return AdminDto.fromJson(doc.data()!).copyWith(id: doc.id);
}
Related