I have a json which is as follows

{
    "status": 1,
    "msg": "Success",
    "data": {
        "lastIndex": 50,
        "studentData": [
            {
                "student_id": "627",
                "stud_name": "A Seth  ",
                "stud_profilepic": "http://assets/default.png",
                "student_email": "[email protected]",
                "student_mobile": "1001",
                "course_name": "Product Design",
                "class_code": "-UG-PD-2019",
                "minAvg": 80,
                "avg": 0
            },
            {
                "student_id": "574",
                "stud_name": "A Ban  ",
                "stud_profilepic": "http://default/default.png",
                "student_email": "[email protected]",
                "student_mobile": "34324324",
                "course_name": "Communication Design",
                "class_code": "CD-2019",
                "minAvg": 80,
                "avg": 80
            },.......

I am using built value for PODO class, following is the code

library student_model;

import 'dart:convert';

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

part 'student_model.g.dart';

abstract class StudentModel implements Built<StudentModel, StudentModelBuilder> {
  StudentModel._();

  factory StudentModel([updates(StudentModelBuilder b)]) = _$StudentModel;

  @BuiltValueField(wireName: 'status')
  int get status;
  @BuiltValueField(wireName: 'msg')
  String get msg;
  @BuiltValueField(wireName: 'data')
  StudentModelData get data;

  static Serializer<StudentModel> get serializer => _$studentModelSerializer;
}

abstract class StudentModelData implements Built<StudentModelData, StudentModelDataBuilder> {
  StudentModelData._();

  factory StudentModelData([updates(StudentModelDataBuilder b)]) = _$StudentModelData;

  @BuiltValueField(wireName: 'lastIndex')
  int get lastIndex;
  @BuiltValueField(wireName: 'studentData')
  BuiltList<StudentData> get studentData;

  static Serializer<StudentModelData> get serializer => _$studentModelDataSerializer;
}

abstract class StudentData implements Built<StudentData,StudentDataBuilder> {
  StudentData._();

  factory StudentData([updates(StudentDataBuilder b)]) = _$StudentData;

  @BuiltValueField(wireName: 'student_id')
  String get studentId;
  @BuiltValueField(wireName: 'stud_name')
  String get studentName;
  @BuiltValueField(wireName: 'stud_profilepic')
  String get studentProfilePicture;
  @BuiltValueField(wireName: 'student_email')
  String get studentEmail;
  @BuiltValueField(wireName: 'course_name')
  String get courseName;
  @BuiltValueField(wireName: 'class_code')
  String get classCode;
  @BuiltValueField(wireName: 'minAvg')
  int get minAverage;
  @BuiltValueField(wireName: 'avg')
  int get average;

  static Serializer<StudentData> get serializer => _$studentDataSerializer;

}

Following is the serializer class

library serializers;
import 'package:built_value/serializer.dart';
import 'package:built_collection/built_collection.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'package:infinite_list/models/student_model.dart';

part 'serializers.g.dart';

@SerializersFor(const [StudentModel,])

Serializers serializers = _$serializers;

Serializers standardSerializers = (serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();

I have runed the flutter packages pub run build_runner build to properly generate code.

Following is my http code

Future<StudentModel> fetchStudents(String disciplineId, String schoolId,
      String year_id, String lastIndex) async {

    final response = await http.post(GET_STUDENTS, body: {

      "schoolId": schoolId,
      "year_id": year_id,

    });

    StudentModel studentModel = serializers.deserializeWith(StudentModel.serializer, json.decode(response.body));

    return studentModel;
  }

But when ever I run the app I get error saying

Deserializing '{status: 1, msg: Success, data: {lastIndex: 50, studentData: [{student_id: 62...' to 'StudentModel' failed due to: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>' in type cast
#0      BuiltJsonSerializers._deserialize (package:built_value/src/built_json_serializers.dart:154:11)
#1      BuiltJsonSerializers.deserialize (package:built_value/src/built_json_serializers.dart:105:18)
#2      BuiltJsonSerializers.deserializeWith (package:built_value/src/built_json_serializers.dart:35:12)
#3      StudentApiProvider.fetchStudents (package:infinite_list/resources/student_api_provider.dart:21:45)
<asynchronous suspension>
#4      Repository.fetchStudents (package:infinite_list/resources/repository.dart:21:35)
<asynchronous suspension>
#5      StudentsBloc.fetchStudents (package:infinite_list/bloc/student_bloc.dart:16:39)
<asynchronous suspension>

The above code works fine when I am using normal PODO class but with built value it gives the error


Solution 1: Pritish

I was using the wrong serializer

Replace this

StudentModel studentModel = serializers.deserializeWith(StudentModel.serializer, json.decode(response.body));

with

StudentModel studentModel = standardSerializers.deserializeWith(StudentModel.serializer, json.decode(response.body));