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

Issue

I want to get a category section from Firebase Firestore so I used this 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,
    };
  }
}

and then I created this with gets

class HomeViewModel extends GetxController {
  ValueNotifier<bool> get loading => _loading;
  ValueNotifier<bool> _loading = ValueNotifier(false);

  List<CategoryModel> get categoryModel => _categoryModel;
  List<CategoryModel> _categoryModel = [];

  HomeViewModel() {
    getCategory();
  }
  getCategory() async {
    _loading.value = true;
    await HomeService().getCategory().then((value) {
      for (int i = 0; i < value.length; i++) {
        _categoryModel.add(
          CategoryModel.fromJson(
            value[i].data(),
          ),
        );
        
        _loading.value = false;
      }
      update();
    });
  }
}

but when I try to get categories from Firestore with the function getCategory() this error comes

Error in vs code

Error in Problems scetion

Solution

Try it this way and tell me if it’s fixed.

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

Answered By – esentis

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.