skip to Main Content

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’;

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;
  }
}

2

Answers


  1. 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

    Login or Signup to reply.
  2. Try this and check if it works:

    List<ContentModel> courseModelFromJson(String str) => List<ContentModel>.from( json.decode(str).map((x) => ContentModel.fromJson(x)).toList());

    If I’m not wrong, the .map() method doesn’t return a List. You need to cast it using .toList().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search