Jamshaid
Jamshaid

Reputation: 410

Unhandled Exception: type 'String' is not a subtype of type 'int' even having no integer in the data

I am getting data from the database using a POST request, and the data is retrieved in JSON format. Here is the data sample:

[
{
    "course_name": "Calculus & Analytical Geometry",
    "batch_name": "FA16-BCS-B16-B"
},
{
    "course_name": "COURSE 3",
    "batch_name": "FA16-BCS-B16-B"
},
{
    "course_name": "COURSE 4",
    "batch_name": "FA16-BCS-B16-B"
},
{
    "course_name": "COURSE 5",
    "batch_name": "FA16-BCS-B16-B"
},
{
    "course_name": "COURSE 6",
    "batch_name": "FA16-BCS-B16-B"
},
{
    "course_name": "Object Oriented Programming",
    "batch_name": "FA16-BCS-B16-B"
}
]

I am having the following parsing class.

class TeacherHomeData {
final String course_name;
final int batch_name;

TeacherHomeData({this.course_name, this.batch_name});

factory TeacherHomeData.fromJson(Map<String, dynamic> json) {
return TeacherHomeData(
  course_name: json['course_name'],
  batch_name: json['batch_name'],
);
}

Map<String, dynamic> toMap() =>
  {"course_name": course_name, "batch_name": batch_name};
}

I am fetching and parsing data like this:

ppUtils.getStringFromPref("teacher_id").then((teacher_id) async {
  final data = await http
      .post(AppUtils.teacherHomeLink, body: {"teacher_id": teacher_id});
  var responseBody = data.body;
 print(responseBody);
  final parsed = json.decode(data.body).cast<Map<String, dynamic>>();

  List<TeacherHomeData> parentSigninList = parsed
      .map<TeacherHomeData>((json) => TeacherHomeData.fromJson(json))
      .toList();

It throws the following error.

E/flutter (19272): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'String' is not a subtype of type 'int'
E/flutter (19272): #0      new TeacherHomeData.fromJson (package:attendance_system/JSONConverterClasses/TeacherHomeData.dart:11:23)
E/flutter (19272): #1      _Teacher_HomeState.jamoo.<anonymous closure>.<anonymous closure> (package:attendance_system/src/TeacherHome.dart:69:59)
E/flutter (19272): #2      MappedListIterable.elementAt (dart:_internal/iterable.dart:417:29)
E/flutter (19272): #3      ListIterable.toList (dart:_internal/iterable.dart:221:19)
E/flutter (19272): #4      _Teacher_HomeState.jamoo.<anonymous closure>     (package:attendance_system/src/TeacherHome.dart:70:12)
E/flutter (19272): <asynchronous suspension>

I am unable to find the logic behind the exception. I am not having any int value in my data then why it is throwing this exception.

Upvotes: 1

Views: 63

Answers (1)

Haroon Ashraf Awan
Haroon Ashraf Awan

Reputation: 1219

You are taking batch_name as integer in your model while it's a string in your json file that's why it is showing a parsing error. Try this:

class TeacherHomeData {
final String course_name;
final String batch_name;

TeacherHomeData({this.course_name, this.batch_name});

factory TeacherHomeData.fromJson(Map<String, dynamic> json) {
return TeacherHomeData(
  course_name: json['course_name'],
  batch_name: json['batch_name'],
);
}

Map<String, dynamic> toMap() =>
  {"course_name": course_name, "batch_name": batch_name};
}

Upvotes: 2

Related Questions