Home > Back-end >  Dart Enums included as Null in Json even if include_if_null: false
Dart Enums included as Null in Json even if include_if_null: false

Time:10-18

This is a new issue I've found and was wondering if anyone else encountered it. Recently in my classes that have an enum field, when I use the toJson() method, any of the enums that are null produce a null in the map, even if they should be ignored. For instance:

import 'package:freezed_annotation/freezed_annotation.dart';

part 'temp.freezed.dart';
part 'temp.g.dart';

enum YesNo { yes, no }

@freezed
class Temp1 with _$Temp1 {
  Temp1._();
  factory Temp1({
    String? name,
    YesNo? yesno,
  }) = _Temp1;

  factory Temp1.fromJson(Map<String, dynamic> json) => _$Temp1FromJson(json);
}

@JsonSerializable(includeIfNull: false)
class Temp2 {
  Temp2({
    this.name,
    this.yesno,
  });

  String? name;
  YesNo? yesno;

  factory Temp2.fromJson(Map<String, dynamic> json) => _$Temp2FromJson(json);
  Map<String, dynamic> toJson() => _$Temp2ToJson(this);
}

If I then run this:

import 'temp.dart';

void main() {
  final temp1 = Temp1(yesno: YesNo.no);
  final temp2 = Temp2(yesno: YesNo.yes);
  print(temp1.toJson());
  print(temp2.toJson());
}

I get:

{yesno: null}
{yesno: null}

I've also tried including a build.yaml file

targets:
    $default:
      builders:
        json_serializable:
          options:
            # Options configure how source code is generated for every
            # `@JsonSerializable`-annotated class in the package.
            #
            # The default value for each is listed.
            any_map: false
            checked: false
            create_factory: true
            create_to_json: true
            disallow_unrecognized_keys: false
            explicit_to_json: true
            field_rename: none
            ignore_unannotated: false
            include_if_null: false

With the same results. I thought maybe it was an issue with freezed or json_serializable, but using older versions of the packages I get the same issue. However, it is definitely a code generation issue. If I look into the temp.g.dart generated from the above file, I see:

Map<String, dynamic> _$Temp2ToJson(Temp2 instance) {
  final val = <String, dynamic>{};

  void writeNotNull(String key, dynamic value) {
    if (value != null) {
      val[key] = value;
    }
  }

  writeNotNull('name', instance.name);
  val['yesno'] = _$YesNoEnumMap[instance.yesno];
  return val;
}

Previously it would have given me something like:

Map<String, dynamic> _$Temp2ToJson(Temp2 instance) {
  final val = <String, dynamic>{};

  void writeNotNull(String key, dynamic value) {
    if (value != null) {
      val[key] = value;
    }
  }

  writeNotNull('name', instance.name);
  writeNotNull('yesno', _$YesNoEnumMap[instance.yesno];
  return val;
}

Anyone else run into this issue?

CodePudding user response:

You are doing things correct. There is a pull request to fix this issue and it seems it is soon live. Just final tests (checking clean merge) that isn't passing:

https://github.com/google/json_serializable.dart/pull/1223

https://github.com/google/json_serializable.dart/commit/5107ac1df814527853ab65686dced776335bf498

  • Related