由于以下原因而失败:错误的状态:BuiltList

时间:2019-02-04 13:27:14

标签: dart flutter

我在builtvalue班上使用PODO

以下是我的json响应

{
    "status": 1,
    "msg": "Success",
    "allotmentMasterID": "1",
    "allotmentInfoID": "1",
    "category": [
        {
            "categoryID": "1",
            "categoryName": "Major",
            "selectedCount": "0",
            "status": 1
        },
        {
            "categoryID": "2",
            "categoryName": "Mandatory",
            "selectedCount": "0",
            "status": 0
        },
        {
            "categoryID": "3",
            "categoryName": "Minor",
            "selectedCount": "0",
            "status": 0
        }
    ]
}

我为此创建了一个built value

以下是课程

library specialisation_model_first_screen;

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

part 'specialisation_model_first_screen.g.dart';

abstract class SpecialisationModelFirstScreen
    implements
        Built<SpecialisationModelFirstScreen,
            SpecialisationModelFirstScreenBuilder> {
  SpecialisationModelFirstScreen._();

  factory SpecialisationModelFirstScreen(
          [updates(SpecialisationModelFirstScreenBuilder b)]) =
      _$SpecialisationModelFirstScreen;

  @nullable
  @BuiltValueField(wireName: 'status')
  int get status;
  @nullable
  @BuiltValueField(wireName: 'msg')
  String get msg;
  @nullable
  @BuiltValueField(wireName: 'allotmentMasterID')
  String get allotmentMasterID;
  @nullable
  @BuiltValueField(wireName: 'allotmentInfoID')
  String get allotmentInfoID;
  @nullable
  @BuiltValueField(wireName: 'category')
  BuiltList<Category> get category;

  static Serializer<SpecialisationModelFirstScreen> get serializer =>
      _$specialisationModelFirstScreenSerializer;
}

abstract class Category implements Built<Category, CategoryBuilder> {
  Category._();

  factory Category([updates(CategoryBuilder b)]) = _$Category;

  @nullable
  @BuiltValueField(wireName: 'categoryID')
  String get categoryID;
  @nullable
  @BuiltValueField(wireName: 'categoryName')
  String get categoryName;
  @nullable
  @BuiltValueField(wireName: 'selectedCount')
  String get selectedCount;
  @nullable
  @BuiltValueField(wireName: 'status')
  int get status;

  static Serializer<Category> get serializer => _$categorySerializer;
}



library serializers;

import 'package:built_value/serializer.dart';

part 'package:dice_clutter/models/serializers/serializers.g.dart';

@SerializersFor(const [SpecialisationModelFirstScreen])

Serializers serializers = _$serializers;

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

以下是我的serializers.g.dart文件

part of serializers;
Serializers _$serializers = (new Serializers().toBuilder()
      ..add(SpecialisationModelFirstScreen.serializer))
    .build();

当我发出api请求时,我正确地获得了响应,但引发了以下错误

failed due to: Bad state: No builder factory for BuiltList<Category>. Fix by adding one, see SerializersBuilder.addBuilderFactory.

这是build value库本身中的错误,还是我做错了什么?

在某些情况下-我还不完全了解具体原因-我的对象无法使用StandartJsonPlugin反序列化。

3 个答案:

答案 0 :(得分:1)

我必须如下编辑我的serializers.g.dart文件

Serializers _$serializers = (new Serializers().toBuilder()
  ..add(SpecialisationModelFirstScreen.serializer)
   ..add(Category.serializer)
  ..addBuilderFactory(
      const FullType(
          BuiltList, const [const FullType(Category)]),
          () => new ListBuilder<Category>())
)
    .build();

感谢Tensor Programming上的Youtube视频 buildvalue库中存在一些错误,因为在某些情况下它无法正确生成serializers.g.dart。希望以后能解决

答案 1 :(得分:0)

基于JSON and serialization - Flutter和您的代码,

Json是您所缺少的,

使用JSON to Dart

请参见fromJson函数

class Autogenerated {
  int status;
  String msg;
  String allotmentMasterID;
  String allotmentInfoID;
  List<Category> category;

  Autogenerated(
      {this.status,
      this.msg,
      this.allotmentMasterID,
      this.allotmentInfoID,
      this.category});

  Autogenerated.fromJson(Map<String, dynamic> json) {
    status = json['status'];
    msg = json['msg'];
    allotmentMasterID = json['allotmentMasterID'];
    allotmentInfoID = json['allotmentInfoID'];
    if (json['category'] != null) {
      category = new List<Category>();
      json['category'].forEach((v) {
        category.add(new Category.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['status'] = this.status;
    data['msg'] = this.msg;
    data['allotmentMasterID'] = this.allotmentMasterID;
    data['allotmentInfoID'] = this.allotmentInfoID;
    if (this.category != null) {
      data['category'] = this.category.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Category {
  String categoryID;
  String categoryName;
  String selectedCount;
  int status;

  Category(
      {this.categoryID, this.categoryName, this.selectedCount, this.status});

  Category.fromJson(Map<String, dynamic> json) {
    categoryID = json['categoryID'];
    categoryName = json['categoryName'];
    selectedCount = json['selectedCount'];
    status = json['status'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['categoryID'] = this.categoryID;
    data['categoryName'] = this.categoryName;
    data['selectedCount'] = this.selectedCount;
    data['status'] = this.status;
    return data;
  }
}

答案 2 :(得分:0)

您需要将Category类添加到可以序列化的类列表中:

@SerializersFor(const [
  SpecialisationModelFirstScreen,
  Category, <-- Category available for serialization
])

然后重新生成您的built_value类。