jsonflutterdartjson-serializablebuild-runner

How to Generate Json_Serializable Dart (Flutter) on Inheritance class?


Hello i have 2 parent class Student and Room, which two of them inheritance class model StudentModel and RoomModel, i am try to generate json converter using Json_Serializable Library on Model Class, but when i try to generate with build_runner its show error like this :

My Code

import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

part 'student.g.dart';

/// * Student & StudentModel

class Student extends Equatable {
  final int id;
  final String name;
  final Room studentRoom;

  const Student({this.id = 0, this.name = "", this.studentRoom = const Room()});

  @override
  List<Object?> get props => [id, name];
}

@JsonSerializable(explicitToJson: true)
class StudentModel extends Student {
  const StudentModel({int id = 0, String name = "", RoomModel studentRoom = const RoomModel()});

  factory StudentModel.fromJson(Map<String, dynamic> json) => _$StudentModelFromJson(json);

  Map<String, dynamic> toJson() => _$StudentModelToJson(this);
}

/// * Room & RoomModel

class Room extends Equatable {
  final int id;
  final String roomName;

  const Room({this.id = 0, this.roomName = ""});

  @override
  List<Object?> get props => [id, roomName];
}

@JsonSerializable()
class RoomModel extends Room {
  const RoomModel({int id = 0, String roomName = ""}) : super(id: id, roomName: roomName);

  factory RoomModel.fromJson(Map<String, dynamic> json) => _$RoomModelFromJson(json);

  Map<String, dynamic> toJson() => _$RoomModelToJson(this);
}

Response Builder

Could not generate `toJson` code for `studentRoom`.
To support the type `Room` you can:
* Use `JsonConverter`
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
* Use `JsonKey` fields `fromJson` and `toJson`
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
package:untitled1/model/student.dart:11:14
   ╷
11 │   final Room studentRoom;
   │              ^^^^^^^^^^^
   ╵

The problem is builder is search for Room toJson and fromJson not RoomModel, i used jsonKey but it doesn't help, how to fix that problem? Thanks.


Solution

  • use following code

    import 'package:equatable/equatable.dart';
    import 'package:json_annotation/json_annotation.dart';
    
    part 'student.g.dart';
    
    class Student extends Equatable {
      final int id;
      final String name;
      final Room studentRoom;
      const Student({this.id = 0, this.name = "", this.studentRoom = const Room()});
    
      @override
      List<Object?> get props => [id, name];
    }
    
    @JsonSerializable(explicitToJson: true)
    class StudentModel extends Student {
      StudentModel({int id = 0, String name = ""});
    
      factory StudentModel.fromJson(Map<String, dynamic> json) =>
          _$StudentModelFromJson(json);
    
      Map<String, dynamic> toJson() => _$StudentModelToJson(this);
    }
    
    /// * Room & RoomModel
    
    class Room extends Equatable {
      final int id;
      final String roomName;
    
      const Room({this.id = 0, this.roomName = ""});
    
      @override
      List<Object?> get props => [id, roomName];
    }
    
    @JsonSerializable()
    class RoomModel extends Room {
      const RoomModel({int id = 0, String roomName = ""})
          : super(id: id, roomName: roomName);
    
      factory RoomModel.fromJson(Map<String, dynamic> json) =>
          _$RoomModelFromJson(json);
    
      Map<String, dynamic> toJson() => _$RoomModelToJson(this);
    }