gpt4 book ai didi

http - 由于 '_InternalLinkedHashMap'不是 'Iterable'类型的子类型,反序列化失败

转载 作者:行者123 更新时间:2023-12-03 04:29:15 24 4
gpt4 key购买 nike

我有一个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时,它给出了错误

最佳答案

我使用了错误的serializer
取代这个

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


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

关于http - 由于 '_InternalLinkedHashMap<String, dynamic>'不是 'Iterable<dynamic>'类型的子类型,反序列化失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54444424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com