Skip to content

factory fromJson and static fromJson provide two different g.dart result #1425

Closed as not planned
@RubyRook

Description

@RubyRook

I want to make code much shorter without use JsonConverter. While factory fromJson cannot return null I need to use static fromJson instead. But, g.dart file didn't provide what i want.

Example with factory:

import 'package:json_annotation/json_annotation.dart';

part 'di.category.g.dart';

@JsonSerializable(explicitToJson: true,)
class DiCategory {
  String id;
  String en_name;
  String km_name;
  String slug;
  String parent;
  bool popular;
  DiIcon? icon;

  DiCategory({
    required this.id,
    required this.en_name,
    required this.km_name,
    required this.slug,
    required this.parent,
    required this.popular,
    this.icon,
  });

  factory DiCategory.fromJson(Map<String, dynamic> json) => _$DiCategoryFromJson(json);
  Map<String, dynamic> toJson()=> _$DiCategoryToJson(this);
}

@JsonSerializable()
class DiIcon {
  String url;
  String width;
  String height;

  DiIcon({
    required this.url,
    required this.width,
    required this.height,
  });

  factory DiIcon.fromJson(Object? json) => _$DiIconFromJson(json is Map<String, dynamic> ? json:{});
  Map<String, dynamic> toJson()=> _$DiIconToJson(this);
}

Inside di.category.g.dart

DiCategory _$DiCategoryFromJson(Map<String, dynamic> json) => DiCategory(
      id: json['id'] as String,
      en_name: json['en_name'] as String,
      km_name: json['km_name'] as String,
      slug: json['slug'] as String,
      parent: json['parent'] as String,
      popular: json['popular'] as bool,
      icon: json['icon'] == null ? null : DiIcon.fromJson(json['icon']),
    );

Example with static:

import 'package:json_annotation/json_annotation.dart';

part 'di.category.g.dart';

@JsonSerializable(explicitToJson: true,)
class DiCategory {
  String id;
  String en_name;
  String km_name;
  String slug;
  String parent;
  bool popular;
  DiIcon? icon;

  DiCategory({
    required this.id,
    required this.en_name,
    required this.km_name,
    required this.slug,
    required this.parent,
    required this.popular,
    this.icon,
  });

  factory DiCategory.fromJson(Map<String, dynamic> json) => _$DiCategoryFromJson(json);
  Map<String, dynamic> toJson()=> _$DiCategoryToJson(this);
}

@JsonSerializable()
class DiIcon {
  String url;
  String width;
  String height;

  DiIcon({
    required this.url,
    required this.width,
    required this.height,
  });

  static DiIcon? fromJson(Object? json) => json is Map<String, dynamic> ? _$DiIconFromJson(json):null;
  Map<String, dynamic> toJson()=> _$DiIconToJson(this);
}

Inside di.category.g.dart

DiCategory _$DiCategoryFromJson(Map<String, dynamic> json) => DiCategory(
      id: json['id'] as String,
      en_name: json['en_name'] as String,
      km_name: json['km_name'] as String,
      slug: json['slug'] as String,
      parent: json['parent'] as String,
      popular: json['popular'] as bool,
      icon: json['icon'] == null
          ? null
          : DiIcon.fromJson(json['icon'] as Map<String, dynamic>),
    );
  • The difference is that, why static fromJson generate with DiIcon.fromJson(json['icon'] as Map<String, dynamic>) and factory fromJson generate with DiIcon.fromJson(json['icon']) while both static and factory fromJson have same parameter data type Object? json
  • The problem is that, it no error when i use with factory if json['icon'] not Map<String, dynamic> but it will when i use with static because DiIcon.fromJson(json['icon'] as Map<String, dynamic>)
  • One more thing, it save me a lot of time if compare with implements JsonConverter while my purpose just check json data type and return null is invalid.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions