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

Map<String, dynamic>' is not a subtype Flutter

i am creating a flutter app with restapi connect when connect api and run the app i got the errror was i attached below please check it. attached the full error below.

══╡ EXCEPTION CAUGHT BY PROVIDER ╞══════════════════════════════════════════════════════════════════
The following assertion was thrown:
An exception was throw by Future<List<ContentModel>> listened by

    FutureProvider<List<ContentModel>>, but no `catchError` was provided.
    
    Exception:
    type '_Map<String, dynamic>' is not a subtype of type 'Iterable<dynamic>'
    
════════════════════════════════════════════════════════════════════════════════════════════════════

D/AwareBitmapCacher( 4843): handleInit switch not opened pid=4843
W/Settings( 4843): Setting device_provisioned has moved from android.provider.Settings.Secure to android.provider.Settings.Global.
V/HiTouch_HiTouchSensor( 4843): User setup is finished.

what i tried so far i attached below. please some one alse solve this probelm

Model : this is Model class i attached below
contents.dart
import ‘dart:convert’;

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<ContentModel> courseModelFromJson(String str) => List<ContentModel>.from(
    json.decode(str).map((x) => ContentModel.fromJson(x)));

String courseModelToJson(List<ContentModel> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class ContentModel {
  int? id;
  String? name;
  int? course_id;
  String? description;
  DateTime? createdAt;
  DateTime? updatedAt;

  ContentModel({
    this.id,
    this.name,
    this.course_id,
    this.description,
    this.createdAt,
    this.updatedAt,
  });

  factory ContentModel.fromJson(Map<String, dynamic> json) => ContentModel(
        id: json["id"],
        name: json["name"],
        course_id: json["course_id"],
        description: json["description"],
        createdAt: json["created_at"] == null
            ? null
            : DateTime.parse(json["created_at"]),
        updatedAt: json["updated_at"] == null
            ? null
            : DateTime.parse(json["updated_at"]),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "course_id": course_id,
        "description": description,
        "created_at": createdAt?.toIso8601String(),
        "updated_at": updatedAt?.toIso8601String(),
      };
}

Api intergration like this

Services : this is Service class i attached below

contents.dart

import 'package:flutter/material.dart';
import 'package:myfirst/infrstructure/models/contents.dart';

import '../helper/api_helper.dart';

final ApiBaseHelper _helper = ApiBaseHelper();

class ContentServices {
  ///Get Courses
  Future<List<ContentModel>> getCourses(BuildContext context) async {
    final response =
        await _helper.get(context, endPoint: '/api/admin/contentapi');
    List<ContentModel> _list = [];
    for (var i in response) {
      _list.add(ContentModel.fromJson(i));
    }
    return _list;
  }
}

>Solution :

it seems like you try to parse the wrong part of your response

the api returns the list of courses in a field called ‘data’ you try to access it from the json root

one possible solution would be to swap

for (var i in response) {
      _list.add(ContentModel.fromJson(i));
    }

with

for (var i in response['data']) {
      _list.add(ContentModel.fromJson(i));
    }

or something similar

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