k-paxian / dart-json-mapper

Serialize / Deserialize Dart Objects to / from JSON
https://pub.dev/packages/dart_json_mapper
Other
400 stars 33 forks source link

Deserializing error: Unable to instantiate class #227

Open RaulUrtecho opened 2 days ago

RaulUrtecho commented 2 days ago

Hi nice package, I hope you can assist me. I have the following models:

@jsonSerializable
class Filters extends Equatable {
  final String? author;
  final List<String>? colDisplay;
  final List<String>? colOrder;
  final String? created;
  final Criteria? criteria;
  @JsonProperty(name: "default")
  final bool? isDefault;
  final String? lastModified;
  final bool? public;
  final List<String>? tags;
  final String? title;
  @JsonProperty(ignoreIfNull: true)
  final String? uuid;

  Filters({
    this.author,
    this.colDisplay = const [],
    this.colOrder = const [],
    this.created,
    this.criteria,
    this.isDefault,
    this.lastModified,
    this.public,
    this.tags,
    this.title,
    this.uuid,
  });

  @override
  @JsonProperty(ignore: true)
  List<Object?> get props =>
      [
        author,
        colDisplay,
        colOrder,
        created,
        criteria,
        isDefault,
        lastModified,
        public,
        tags,
        title,
        uuid,
      ];

  @override
  @JsonProperty(ignore: true)
  bool? get stringify => super.stringify;

  Filters copyWith({
    String? author,
    List<String>? colDisplay,
    List<String>? colOrder,
    String? created,
    Criteria? criteria,
    bool? isDefault,
    String? lastModified,
    bool? public,
    List<String>? tags,
    String? title,
    String? uuid,
  }) {
    return Filters(
      author: author ?? this.author,
      colDisplay: colDisplay ?? this.colDisplay,
      colOrder: colOrder ?? this.colOrder,
      created: created ?? this.created,
      criteria: criteria ?? this.criteria,
      isDefault: isDefault ?? this.isDefault,
      lastModified: lastModified ?? '${DateTime.now().toUtc().asBackendDate}Z',
      public: public ?? this.public,
      tags: tags ?? this.tags,
      title: title ?? this.title,
      uuid: uuid ?? this.uuid,
    );
  }

  static Filters empty({
    required String title,
    required bool public,
    required bool isDefault,
  }) => Filters(title: title, public: public, isDefault: isDefault);
}

@jsonSerializable
class Criteria {
  final String? sortBy;
  final String? sortDirection;
  final List<Sort>? sorts;
  final int? rows;
  @JsonProperty(name: "q")
  final String? query;
  @JsonProperty(name: "fqs")
  final List<Filter> filters;

  Criteria({this.sortBy, this.sortDirection, this.sorts, this.rows, this.query, this.filters = const []});

  Criteria copyWith({
    String? sortBy,
    String? sortDirection,
    List<Sort>? sorts,
    int? rows,
    String? query,
    List<Filter>? filters,
  }) {
    return Criteria(
      sortBy: sortBy ?? this.sortBy,
      sortDirection: sortDirection ?? this.sortDirection,
      sorts: sorts ?? this.sorts,
      rows: rows ?? this.rows,
      query: query ?? this.query,
      filters: filters ?? this.filters,
    );
  }
}

@jsonSerializable
class Sort {
  final String? field;
  final String? direction;

  Sort(this.field, this.direction);

  @override
  @JsonProperty(ignore: true)
  List<Object?> get props => [field, direction];
}

@jsonSerializable
class Filter extends Equatable{
  final String? field;
  final String? operator;
  final dynamic value;

  Filter(this.field, this.operator, this.value);

  @override
  @JsonProperty(ignore: true)
  List<Object?> get props => [field, value];

  @override
  @JsonProperty(ignore: true)
  bool? get stringify => super.stringify;

  @JsonProperty(ignore: true)
  bool get not => operator?.startsWith('n') == true;

  @JsonProperty(ignore: true)
  String get filterOperation {
    return '';
  }
}

When running my app on Android and deserializing I encounter the following error:

(Unable to instantiate class 'Criteria' with null named arguments [Symbol("sortBy"), Symbol("sortDirection")])

I have already tried several things in the model criteria such as: changing from mutable to immutable, positional instead of named, default values ​​in properties and constructs, using non-nullables, and nothing has worked for me, except when I removed the renamed fields

  @JsonProperty(name: "q")
  final String? query;
  @JsonProperty(name: "fqs")
  final List<Filter> filters;

which is very strange.

I would appreciate any insights you can offer as to whether this error is on my end or if something was overlooked.

k-paxian commented 1 day ago

Can you also show me the JSON input you are trying to deserialize so it would be easier to create a unit test out of your case