Я получаю данные с онлайн-терминала. Я использую build_value для десериализации JSON. Однако я пришел к ошибке, и я застрял. Это ошибка, которую я получаю:
Unhandled Exception: Deserializing '[count, 2, results, [{id: 2, name: Upload Purchase orders, kpi: {id: 2, name:...' to 'TasksListResponseJModel' failed due to: Deserializing '[{id: 2, name: Upload Purchase orders, kpi: {id: 2, name: kpi}, assign: {id: ...' to 'BuiltList<Taskresult>' failed due to: Deserializing '[id, 1, name, Telkom, kpi, {id: 1, name: Telkom}, assign, {id: 2, name: cha c...' to 'Taskresult' failed due to: Deserializing '[tag]' to 'BuiltList<Tags>' failed due to: Deserializing 'tag' to 'Tags' failed due to: type 'String' is not a subtype of type 'Iterable<dynamic>' in type cast
JSON:
{
"count": 2,
"next": null,
"previous": null,
"results": [
{
"id": 2,
"name": "Upload Purchase orders",
"kpi": {
"id": 2,
"name": "kpi"
},
"assign": {
"id": 2,
"name": "cha cha"
},
"team": {
"id": 1,
"name": "Commercial"
},
"site": {
"id": 8,
"name": "Soi"
},
"status": "Todo",
"tags": [],
"subtasks": [],
"progress": 0,
"files": [],
"comments": []
},
{
"id": 1,
"name": "Telkom",
"kpi": {
"id": 1,
"name": "Telkom"
},
"assign": {
"id": 2,
"name": "cha cha"
},
"team": {
"id": 1,
"name": "Commercial"
},
"site": {
"id": 1,
"name": "kasa"
},
"status": "Todo",
"tags": [
"tag"
],
"subtasks": [],
"progress": 0,
"files": [],
"comments": []
}
]
}
Это как я реализовал модель:
My Tasks_Model.dart
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:built_collection/built_collection.dart';
part 'TasksListResponseJModel.g.dart';
abstract class TasksListResponseJModel implements Built<TasksListResponseJModel, TasksListResponseJModelBuilder> {
static Serializer<TasksListResponseJModel> get serializer => _$tasksListResponseJModelSerializer;
TasksListResponseJModel._();
@BuiltValueField(wireName: 'count')
int get count;
@BuiltValueField(wireName: 'next')
String get next;
@BuiltValueField(wireName: 'previous')
String get previous;
@BuiltValueField(wireName: 'results')
BuiltList<Taskresult> get results;
factory TasksListResponseJModel([void Function(TasksListResponseJModelBuilder) updates]) = _$TasksListResponseJModel;
}
abstract class Taskresult implements Built<Taskresult, TaskresultBuilder> {
Taskresult._();
factory Taskresult([updates(TaskresultBuilder b)]) = _$Taskresult;
@nullable
@BuiltValueField(wireName: 'id')
int get id;
@nullable
@BuiltValueField(wireName: 'name')
String get name;
@BuiltValueField(wireName: 'kpi')
Kpi get kpi;
@BuiltValueField(wireName: 'assign')
Assign get assign;
@BuiltValueField(wireName: 'team')
Team get team;
@BuiltValueField(wireName: 'site')
Site get site;
@BuiltValueField(wireName: 'progress')
int get progress;
@BuiltValueField(wireName: 'status')
String get status;
@BuiltValueField(wireName: 'tags')
BuiltList<Tags> get tags;
@BuiltValueField(wireName: 'subtasks')
BuiltList<Subtasks> get subtasks;
@BuiltValueField(wireName: 'files')
BuiltList<Files> get files;
@BuiltValueField(wireName: 'comments')
BuiltList<Comments> get comments;
static Serializer<Taskresult> get serializer => _$taskresultSerializer;
}
abstract class Tags implements Built<Tags, TagsBuilder> {
Tags._();
factory Tags([updates(TagsBuilder b)]) = _$Tags;
@nullable
@BuiltValueField(wireName: 'tag')
String get tag;
static Serializer<Tags> get serializer => _$tagsSerializer;
}
abstract class Subtasks implements Built<Subtasks, SubtasksBuilder> {
Subtasks._();
factory Subtasks([updates(SubtasksBuilder b)]) = _$Subtasks;
@nullable
@BuiltValueField(wireName: 'subtask')
String get subtask;
static Serializer<Subtasks> get serializer => _$subtasksSerializer;
}
abstract class Files implements Built<Files, FilesBuilder> {
Files._();
factory Files([updates(FilesBuilder b)]) = _$Files;
@nullable
@BuiltValueField(wireName: 'file')
String get file;
static Serializer<Files> get serializer => _$filesSerializer;
}
abstract class Comments implements Built<Comments, CommentsBuilder> {
Comments._();
factory Comments([updates(CommentsBuilder b)]) = _$Comments;
@nullable
@BuiltValueField(wireName: 'comment')
String get comment;
static Serializer<Comments> get serializer => _$commentsSerializer;
}
abstract class Kpi implements Built<Kpi, KpiBuilder> {
Kpi._();
factory Kpi([void Function(KpiBuilder) updates]) = _$Kpi;
@nullable
@BuiltValueField(wireName: 'id')
int get id;
@nullable
@BuiltValueField(wireName: 'name')
String get name;
static Serializer<Kpi> get serializer => _$kpiSerializer;
}
abstract class Assign implements Built<Assign, AssignBuilder> {
Assign._();
static Serializer<Assign> get serializer => _$assignSerializer;
@nullable
@BuiltValueField(wireName: 'id')
int get id;
@nullable
@BuiltValueField(wireName: 'name')
String get name;
factory Assign([void Function(AssignBuilder) updates]) = _$Assign;
}
abstract class Site implements Built<Site, SiteBuilder> {
Site._();
static Serializer<Site> get serializer => _$siteSerializer;
@nullable
@BuiltValueField(wireName: 'id')
int get id;
@nullable
@BuiltValueField(wireName: 'name')
String get name;
factory Site([void Function(SiteBuilder) updates]) = _$Site;
}
abstract class Team implements Built<Team, TeamBuilder> {
Team._();
static Serializer<Team> get serializer => _$teamSerializer;
@nullable
@BuiltValueField(wireName: 'id')
int get id;
@nullable
@BuiltValueField(wireName: 'name')
String get name;
factory Team([void Function(TeamBuilder) updates]) = _$Team;
}
У меня есть FutureBuilder, который получает задачи здесь:
Future<FutureBuilder<Response>> _buildBody(BuildContext context) async {
return FutureBuilder<Response<BuiltList<TasksListResponseJModel>>>(
future: Provider.of<ApiService>(context).getTasks(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Center(
child: Text(
snapshot.error.toString(),
textAlign: TextAlign.center,
textScaleFactor: 1.3,
),
);
}
final posts = snapshot.data.body;
return _buildPosts(context, posts);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
);
}
Я был бы очень признателен за объяснение того, где я иду не так. Заранее спасибо.