Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'OriginDestination' in type cast int flutter

I am trying to fetch data from a local json file in my assets. I received and decoded data and then tried to store it in a list for further use. But it give me the exception. I want the list to be of type OriginDestination, store it in database and then use it further. Can someone please tell me how can I parse data from json to OriginDestination.

Class OriginDestination –

OriginDestination cityDtoFromJson(String str) => OriginDestination.fromJson(json.decode(str));

String cityDtoToJson(OriginDestination data) => json.encode(data.toJson());

@HiveType(typeId: 0)
// ignore: non_constant_identifier_names
class OriginDestination extends HiveObject {

  @HiveField(0)
  City? origin;

  @HiveField(1)
  List<City>? destinations;

  OriginDestination({
    this.origin,
    this.destinations,
  });

  factory OriginDestination.fromJson(Map<String, dynamic> json) => OriginDestination(
    origin: City.fromJson(json["origin"]),
    destinations: List<City>.from(
        json["destinations"].map((x) => City.fromJson(x))),
  );

  Map<String, dynamic> toJson() => {
    "origin": origin,
    "destinations": destinations,
  };

Code where I am fetching data and want to use it (originDestinations is also a list of type OriginDestination) –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

List<OriginDestination>? localOriginDestination = [];

  Future<void> readJson() async {
    final String response = await rootBundle.loadString('assets/files/node.json');
    final data = await json.decode(response);
    localOriginDestination = await data["data"].cast<OriginDestination>();
    print(localOriginDestination);
    if(localOriginDestination!=null) {
      await _localStorageService.getBox().clear();
      await _localStorageService.getBox().addAll(localOriginDestination!.toList());
      originDestinations = _localStorageService.getOriginDestinations();
    }
  }

>Solution :

I have never used Hive before but this line of code seems suspicious to me:

localOriginDestination = await data["data"].cast<OriginDestination>();

I think you want to do something along the lines of:

localOriginDestination = [
  for (final element in await data['data']) OriginDestination.fromJson(element),
];

But I can’t be entirely certain without knowing what the value of await data['data'] is.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading