我有一个如下的json
{
"status": 1,
"msg": "Success",
"data": {
"lastIndex": 50,
"studentData": [
{
"student_id": "627",
"stud_name": "A Seth ",
"stud_profilepic": "http://assets/default.png",
"student_email": "aeth@net.in",
"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": "aasal@net.in",
"student_mobile": "34324324",
"course_name": "Communication Design",
"class_code": "CD-2019",
"minAvg": 80,
"avg": 80
},.......
我正在将built value
类用于PODO
,下面是代码
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;
}
以下是serializer
类
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();
我已经运行flutter packages pub run build_runner build
来正确生成代码。
以下是我的http代码
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;
}
但是,每当我运行该应用程序时,都会出现错误提示
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>
当我使用普通的PODO
类但使用built value
时,上面的代码可以正常工作
答案 0 :(得分:0)
我使用了错误的serializer
替换此
StudentModel studentModel = serializers.deserializeWith(StudentModel.serializer, json.decode(response.body));
使用
StudentModel studentModel = standardSerializers.deserializeWith(StudentModel.serializer, json.decode(response.body));