Search code examples
jsonflutterdart

Ignore required fields of a nullable referenced class when parsing JSON in Flutter Dart


I'm currently trying to display a class that references multiple other classes but are nullable because it's not always sure which one of the referenced classes would have a value. Now, because of Dart's aggressive action towards trying to load all the required stuff you declared in your model class, it wants to load all the fields of the nullable classes, which I don't really want to do. What can I do to limit this actions? It was alright when I have to deal with those referenced classes by themselves, but it's really annoying this time because I already set them as nullable and they are still trying to be loaded.

Here's my code so far:

Here's the model I'm referring in my question:

import 'package:json_annotation/json_annotation.dart';

part 'delivery.g.dart';

enum DeliveryStatus {
  @JsonValue(0)ToBeConfirmed,
  @JsonValue(1)Ongoing,
  @JsonValue(2)Received,
  @JsonValue(3)Cancelled,
}

@JsonSerializable(explicitToJson: true)
class Delivery{
  Delivery({
    this.id,
    this.shippingDoc,
    this.shippingDocId,
    this.vhc,
    this.vhcId,
    this.aic,
    this.aicId,
    required this.status,
    required this.receivedOnDatetime,
    this.receivedBy,
    required this.receivedById,
    this.remarks,
    this.isConfirmed,
    this.isCancelled,
    this.confirmedOnDatetime,
    this.cancelledOnDatetime,
  });

  @JsonKey(name: 'id')
  String? id = "";

  @JsonKey(name: 'shipping_doc')
  ShippingDoc? shippingDoc;

  @JsonKey(name: 'shipping_doc_id')
  String? shippingDocId = "";

  @JsonKey(name: 'vhc')
  VhcApplication? vhc;

  @JsonKey(name: 'vhc_id')
  String? vhcId = "";

  @JsonKey(name: 'aic')
  VhcApplication? aic;

  @JsonKey(name: 'aic_id')
  String? aicId = "";

  @JsonKey(name: 'status')
  DeliveryStatus status = DeliveryStatus.ToBeConfirmed;

  @JsonKey(name: 'received_on_datetime')
  String receivedOnDatetime = '';

  @JsonKey(name: 'received_by')
  User? receivedBy;

  @JsonKey(name: 'received_by_id')
  String receivedById = "";

  @JsonKey(name: 'remarks')
  String? remarks = "";

  @JsonKey(name: 'is_confirmed')
  bool? isConfirmed = false;

  @JsonKey(name: 'is_cancelled')
  bool? isCancelled = false;

  @JsonKey(name: 'confirmed_on_datetime')
  String? confirmedOnDatetime = "";

  @JsonKey(name: 'cancelled_on_datetime')
  String? cancelledOnDatetime = "";

  factory Delivery.fromJson(Map<String, dynamic> json) => _$DeliveryFromJson(json);
  Map<String, dynamic> toJson() => _$DeliveryToJson(this);
}

And the referenced classes are ShippingDoc VhcApplication and VhcApplication(I haven't implemented the real class for aic). Right now, ShippingDoc has value because I made it to have one. VhcApplication, on the other hand, is and should be null, and is what's causing the issue because it is null but Dart still wants to load it's fields


Solution

  • You can update the JsonKey annotation and use includeFromJson: false and/or includeToJson: false

    Like this:

    @JsonKey(includeFromJson: false, includeToJson: false)
      VhcApplication? vhc;
    

    Reference: https://pub.dev/documentation/json_annotation/4.9.0/json_annotation/JsonKey-class.html