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

I am trying to fetch data from backend and store it in my app. However when I am using method to fetch it i am facing this error Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'StartChapter'. Is my model set up wrong or the fetching function is written wrong? Can someone advice me something? Thanks

Method that fetches data:

  Future<List<Adventures>> getAdventureList() async {
    var response = await getAdventures(apiClient);
    return List<Adventures>.from((response.data["collection"]).map((json) => Adventures.fromJson(json)));
  }

I am trying to fetch data from my backend, example JSON looks like:

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

{
   "collection":[
      {
         "completed":true,
         "creator_id":"4b94b85f-696e-4682-9534-572d1b2bc47f",
         "creator_nick":"master_of_adventure69",
         "description":"go on a wonderful adventure through the streets of the old town ...",
         "favorite":true,
         "id":"ffcc3fad-8bc2-4ba3-9b8e-59d77b2bff23",
         "name":"The best adventure in the world",
         "start_chapter":{
            "completed":true,
            "description":"at this point you must demonstrate extraordinary intelligence ...",
            "id":"5130dd90-9738-4a26-aeea-10d7d9c905b5",
            "name":"first point in adventure",
            "position":{
               "lat":53.01379,
               "lon":18.598444
            },
            "question":[
               {
                  "chapter_id":"5130dd90-9738-4a26-aeea-10d7d9c905b5",
                  "description":"What has a tail and barks?",
                  "id":"3718f6a9-fff1-4a15-9a24-ae3fa90b4141",
                  "sort":1,
                  "type":"tip/question"
               }
            ],
            "radius":500,
            "ridle":{
               "answer":"42856",
               "id":"ce8545e6-eeca-41c0-9026-9084df5aad13",
               "type":"number_lock_5"
            },
            "tips":[
               {
                  "chapter_id":"5130dd90-9738-4a26-aeea-10d7d9c905b5",
                  "description":"What has a tail and barks?",
                  "id":"3718f6a9-fff1-4a15-9a24-ae3fa90b4141",
                  "sort":1,
                  "type":"tip/question"
               }
            ]
         }
      }
   ],
   "page":1,
   "page_size":50,
   "total_pages":1
}

Model

class Adventure {
  List<Adventures>? collection;
  int page;
  int page_size;
  int total_pages;
  int total_results;

  Adventure({ this.collection, required this.page, required this.page_size, required this.total_pages,
     required this.total_results });

  factory Adventure.fromJson(Map<String, dynamic> json) {
    return Adventure(
      collection: json['collection'].map((item) => Adventures.fromJson(item)).toList(),
      page: json['page'],
      page_size: json['page_size'],
      total_pages: json['total_pages'],
      total_results: json['total_results']
    );
  }
}

class Adventures {
  bool completed;
  String creatorId;
  String creatorNick;
  String? description;
  bool favorite;
  String id;
  String name;
  StartChapter startChapter;

  Adventures(
      {required this.completed,
      required this.creatorId,
      required this.creatorNick,
        this.description,
      required this.favorite,
      required this.id,
      required this.name,
      required this.startChapter});

  factory Adventures.fromJson(Map<String, dynamic> json) {
    return Adventures(
        completed: json['completed'],
        creatorId: json['creator_id'],
        creatorNick: json['creator_nick'],
        description: json['description'],
        favorite: json['favorite'],
        id: json['id'],
        name: json['name'],
        startChapter: json['start_chapter']
    );
  }
}
and so on

>Solution :

If you have a already StartChapter model and ‘fromJson’ method,
try to change like below.

class Adventures {
  bool completed;
  String creatorId;
  String creatorNick;
  String? description;
  bool favorite;
  String id;
  String name;
  StartChapter startChapter;

  Adventures(
      {required this.completed,
      required this.creatorId,
      required this.creatorNick,
        this.description,
      required this.favorite,
      required this.id,
      required this.name,
      required this.startChapter});

  factory Adventures.fromJson(Map<String, dynamic> json) {
    return Adventures(
        completed: json['completed'],
        creatorId: json['creator_id'],
        creatorNick: json['creator_nick'],
        description: json['description'],
        favorite: json['favorite'],
        id: json['id'],
        name: json['name'],
        startChapter: json['start_chapter'] != null ? StartChapter.fromJson(json['start_chapter']) : null
    );
  }
}

If not, define ‘fromJson’ into the StartChapter class.
Or change startChapter’s type from ‘StartChapter’ to ‘Map<String, dynamic>’ class.

class Adventures {
  bool completed;
  String creatorId;
  String creatorNick;
  String? description;
  bool favorite;
  String id;
  String name;
  Map<String, dynamic> startChapter;
...
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