Home > Software design >  type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Assesm
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Assesm

Time:11-17

Why do I get below exception when calling getAssesment() method?

I'm trying to set _assesment = data

type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'AssesmentModel?'

These are my codes, the models (AssesmentModel & AreaModel) and the provider (AssesmentProvider).

AssesmentModel.dart

class AssesmentModel {
  late int id;
  late String observerName;
  late String unit;
  late AreaModel area;

  AssesmentModel(
    this.id,
    this.observerName,
    this.unit,
    this.area,
  );

  AssesmentModel.fromJson(Map<String, dynamic> json) {
    id = int.parse(json['id'].toString());
    observerName = json['observerName'];
    unit = json['unit'];
    area = AreaModel.fromJson(json['area']);
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'observerName': observerName,
      'unit': unit,
      'area': area.toJson(),
    };
  }
}

AssesmentProvider.dart

class AssesmentFormProvider with ChangeNotifier {

  AssesmentModel? _assesment;

  AssesmentModel? get assesment => _assesment;

  set assesment(AssesmentModel? assesment) {
    _assesment = assesment;
    notifyListeners();
  }
    
  Future<void> getAssesment() async {
    try {
      // AssesmentModel assesment = await AssesmentService().readJson();
      var response = await rootBundle.loadString('assets/json/assesment.json');
      print('the response: '   response);

      var data = json.decode(response);
      print('assesment in provider');
      print(data);

      _assesment = data;
    } catch (e) {
      print(e);
    }
  }
}

This is my AreaModel that relate to AssesmentModel

class AreaModel {
  late int id;
  late String name;

  AreaModel(
    this.id,
    this.name,
  );

  AreaModel.fromJson(Map<String, dynamic> json) {
    id = int.parse(json['id'].toString());
    name = json['name'];
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
    };
  }
}

Thank you for the reply. Very apreciated.

CodePudding user response:

What you get from the response is actually a json and you are trying to assign a json to a custom object. You need to create an object from the response data. I see that you already have .fromJson method in your AssestmentModel class so what you can do is simply;

    class AssesmentFormProvider with ChangeNotifier {

  AssesmentModel? _assesment;

  AssesmentModel? get assesment => _assesment;

  set assesment(AssesmentModel? assesment) {
    _assesment = assesment;
    notifyListeners();
  }
    
  Future<void> getAssesment() async {
    try {
      // AssesmentModel assesment = await AssesmentService().readJson();
      var response = await rootBundle.loadString('assets/json/assesment.json');
      print('the response: '   response);

      var data = json.decode(response);
      print('assesment in provider');
      print(data);
//you need to create an Assetsment Object and then assign it!
      _assesment = AssetsmentModel.fromJson(data);
    } catch (e) {
      print(e);
    }
  }
}

and also you need to modify your .fromJson method to return an Assessment object;

            class AssesmentModel {
      late int id;
      late String observerName;
      late String unit;
      late AreaModel area;
    
      AssesmentModel(
        this.id,
        this.observerName,
        this.unit,
        this.area,
      );
    
      AssesmentModel.fromJson(Map<String, dynamic> json) {
    return AssetsmentModel(
id : int.parse(json['id'].toString()),
        observerName : json['observerName'],
        unit : json['unit'],
        area : AreaModel.fromJson(json['area'])
);
        
      }
    
      Map<String, dynamic> toJson() {
        return {
          'id': id,
          'observerName': observerName,
          'unit': unit,
          'area': area.toJson(),
        };
      }
    }

CodePudding user response:

There are few errors like on parser like on AreaModel doesn't have name, instead it contains area, also some typo errors, go through with this code, and you will get the concept.


class AreaModel {
  final int id;
  final String area;
  AreaModel({
    required this.id,
    required this.area,
  });

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'area': area,
    };
  }

  factory AreaModel.fromMap(Map<String, dynamic> map) {
    return AreaModel(
      id: map['id'],
      area: map['area'],
    );
  }

  String toJson() => json.encode(toMap());

  factory AreaModel.fromJson(String source) =>
      AreaModel.fromMap(json.decode(source));
}

class AssesmentModel {
  final int id;
  final String observer_name;
  final String unit;
  final AreaModel area;

  AssesmentModel({
    required this.id,
    required this.observer_name,
    required this.unit,
    required this.area,
  });

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'observer_name': observer_name,
      'unit': unit,
      'area': area.toMap(),
    };
  }

  factory AssesmentModel.fromMap(Map<String, dynamic> map) {
    return AssesmentModel(
      id: map['id'],
      observer_name: map['observer_name'],
      unit: map['unit'],
      area: AreaModel.fromMap(map['area']),
    );
  }

  String toJson() => json.encode(toMap());

  factory AssesmentModel.fromJson(String source) =>
      AssesmentModel.fromMap(json.decode(source));
}

main(List<String> args) {
  final jsonString =
      '{ "id": 1, "observer_name": "Duffy Jhon", "unit": "Belawan - Refinery", "area": { "id": 1, "area": "Area-1" } }';

  final m = AssesmentModel.fromMap(jsonDecode(jsonString));

  print(m.id);
}

  • Related