The argument type 'Object?' can't be assigned to the parameter type 'Map<dynamic, dynamic>'. GetX

Issue

so basically i’m frustrated by this ‘Object?’ type, i already tried t change its type in core file but i was worried to cause a weird behavior in the future. heres my code any kind of help will be appreciated

class HomeViewModel extends GetxController{

List<CategoryModel> get categorymodel => _categorymodel;
late DocumentSnapshot doc;

List<CategoryModel> _categorymodel = [];

final CollectionReference _categoryCollectionRef = 
FirebaseFirestore.instance.collection('categories');

HomeViewModel(){
getCategory();
}

getCategory()async{
_categoryCollectionRef.get().then((value) {
  for(int i = 0; i<value.docs.length;i++){
    _categorymodel.add(CategoryModel.fromJson(value.docs[i].data()));
  }
});
}

}

and this one is from my model class:

class CategoryModel {
late String name, image;

CategoryModel({required this.name, required this.image});

CategoryModel.fromJson(Map<dynamic, dynamic> map) {
if (map == null) {
  return;
}
name = map['name'];
image = map['image'];
}

toJson() {
return {
  'name': name,
  'image': image,
};
}
}

Solution

You need to specify the type of data you’re expecting from the DocumentSnapshot.

Change this line:

  _categorymodel.add(CategoryModel.fromJson(value.docs[i].data()));

to this:

  _categorymodel.add(CategoryModel.fromJson(value.docs[i].data() as Map<String, dynamic>));

Check out the guide for migration to cloud_firestore 2.0.0.

Answered By – Victor Eronmosele

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.