反序列化的未知类型。需要指定的类型或鉴别字段

时间:2019-03-10 08:14:21

标签: dart flutter

我有以下json

{
    "status": 1,
    "msg": "Success",
    "data": {
        "getFeeCount": [
            {
                "yearId": "1",
                "yearName": "FY 2017-18"
            },
            {
                "yearId": "2",
                "yearName": "FY 2018-19"
            }
        ],
        "getFeeCat": [
            {
                "fee_cat_id": "1",
                "fee_cat_name": "1st Semester Tuition Fee"
            },
            {
                "fee_cat_id": "2",
                "fee_cat_name": "2nd Semester Tuition Fee"
            },
            {
                "fee_cat_id": "3",
                "fee_cat_name": "3rd Semester Tuition Fee"
            },
            {
                "fee_cat_id": "4",
                "fee_cat_name": "4th Semester Tuition Fee"
            },
            {
                "fee_cat_id": "5",
                "fee_cat_name": "5th Semester Tuition Fee"
            },
            {
                "fee_cat_id": "6",
                "fee_cat_name": "6th Semester Tuition Fee"
            },
            {
                "fee_cat_id": "7",
                "fee_cat_name": "7th Semester Tuition Fee"
            },
            {
                "fee_cat_id": "8",
                "fee_cat_name": "8th Semester Tuition Fee"
            },
            {
                "fee_cat_id": "9",
                "fee_cat_name": "Certification Fee Year 1"
            },
            {
                "fee_cat_id": "10",
                "fee_cat_name": "Certification Fee Year 2"
            },
            {
                "fee_cat_id": "11",
                "fee_cat_name": "Certification Fee Year 3"
            },
            {
                "fee_cat_id": "12",
                "fee_cat_name": "Certification Fee Year 4"
            }
        ]
    }
}

我在项目中使用了内置价值

library dashboard_fiscal_year_model;

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

part 'dashboard_fiscal_year_model.g.dart';

abstract class DashboardFiscalYearModel
    implements
        Built<DashboardFiscalYearModel, DashboardFiscalYearModelBuilder> {
  DashboardFiscalYearModel._();

  factory DashboardFiscalYearModel(
          [updates(DashboardFiscalYearModelBuilder b)]) =
      _$DashboardFiscalYearModel;

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

  static Serializer<DashboardFiscalYearModel> get serializer =>
      _$dashboardFiscalYearModelSerializer;
}

abstract class DashboardFiscalYearData
    implements Built<DashboardFiscalYearData, DashboardFiscalYearDataBuilder> {
  DashboardFiscalYearData._();

  factory DashboardFiscalYearData([updates(DashboardFiscalYearDataBuilder b)]) =
      _$DashboardFiscalYearData;

  @BuiltValueField(wireName: 'getFeeCount')
  BuiltList<GetFeeCount> get getFeeCount;
  @BuiltValueField(wireName: 'getFeeCat')
  BuiltList<GetFeeCat> get getFeeCat;

  static Serializer<DashboardFiscalYearData> get serializer =>
      _$dashboardFiscalYearDataSerializer;
}

abstract class GetFeeCount implements Built<GetFeeCount, GetFeeCountBuilder> {
  GetFeeCount._();

  factory GetFeeCount([updates(GetFeeCountBuilder b)]) = _$GetFeeCount;

  @nullable
  @BuiltValueField(wireName: 'yearId')
  String get yearId;
  @nullable
  @BuiltValueField(wireName: 'yearName')
  String get yearName;

  static Serializer<GetFeeCount> get serializer => _$getFeeCountSerializer;
}

abstract class GetFeeCat implements Built<GetFeeCat, GetFeeCatBuilder> {
  GetFeeCat._();

  factory GetFeeCat([updates(GetFeeCatBuilder b)]) = _$GetFeeCat;

  @nullable
  @BuiltValueField(wireName: 'fee_cat_id')
  String get feeCatId;
  @nullable
  @BuiltValueField(wireName: 'fee_cat_name')
  String get feeCatName;
}

但是每当我运行我的应用程序时,都会出现以下错误

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: Deserializing '[status, 1, msg, Success, data, {getFeeCount: [{yearId: 1, yearName: FY 2017-...' to 'DashboardFiscalYearModel' failed due to: Deserializing '[getFeeCount, [{yearId: 1, yearName: FY 2017-18}, {yearId: 2, yearName: FY 20...' to 'DashboardFiscalYearData' failed due to: Deserializing '[{fee_cat_id: 1, fee_cat_name: 1st Semester Tuition Fee}, {fee_cat_id: 2, fee...' to 'BuiltList<GetFeeCat>' failed due to: Invalid argument(s): Unknown type on deserialization. Need either specifiedType or discriminator field.
#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      DashboardDropDownApiProvider.getFiscalYear (package:dice_admin/resources/dashboard/dashboard_fiscal_year/dashboard_fi<…>
[VERBOSE-2:shell.cc(178)] Dart Error: Unhandled exception:
'dart:isolate/runtime/libtimer_impl.dart': Failed assertion: line 433: '<optimized out>': is not true.
#0      _AssertionError._doThrowNew (dart:core/runtime/liberrors_patch.dart:40:39)
#1      _AssertionError._throwNew (dart:core/runtime/liberrors_patch.dart:36:5)
#2      _Timer._cancelWakeup (dart:isolate/runtime/libtimer_impl.dart:433:12)
#3      _Timer._notifyEventHandler (dart:isolate/runtime/libtimer_impl.dart:299:7)
#4      _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:419:5)
#5      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)

我已经多次重新生成代码,并且我认为序列化程序也已正确映射。

我浏览了这篇文章https://github.com/google/built_value.dart/issues/368 它使用了自定义插件,已经建立了价值解决了这个问题,还是我需要使用自定义的价值

0 个答案:

没有答案