Open burhankhanzada opened 8 months ago
after cloning the repo and running the 9 Change To
test in example/readme_test.dart
your test passes while i have done the same thing the difference is you defined both Pet = superclass
, Cat = subclass
in same file at readme_test.dart while i defined each class in its own file may be this causing issue and we are not importing subclass.g.dart
file in our superclass.morphy.dart
Hi @burhankhanzada thanks for trying out Morphy. Yes, if you use the explicitSubtypes
, then you must create the classes in the same file. Would that fix the problem for you?
I would like to at some point look at a way of getting it to work without that. I'll update the readme, it does say same file somewhere in there but it should be more obvious.
Perhaps I should run a check and show a warning at least when we build our code.
I haved
Human
as superclass andStudent
as subclass and defined Student as explicitSubTypes toHuman
morphy annotation options with generateJson to true but getting error for_$StudentFromJson
part 'human.g.dart'; part 'human.morphy.dart';
@Morphy(generateJson: true, explicitSubTypes: [$Student]) abstract class $Human { String get name; int get age; }
part of 'human.dart';
// ** // Generator: MorphyGenerator
// **
///
@JsonSerializable( explicitToJson: true, ) class Human extends $Human { final String name; final int age;
/// Human({ required this.name, required this.age, }); Human._({ required this.name, required this.age, }); String toString() => "(Human-name:${name.toString()}|age:${age.toString()})"; int get hashCode => hashObjects([name.hashCode, age.hashCode]); bool operator ==(Object other) => identical(this, other) || other is Human && runtimeType == other.runtimeType && name == other.name && age == other.age; Human copyWithHuman({ String Function()? name, int Function()? age, }) { return Human.( name: name == null ? this.name as String : name() as String, age: age == null ? this.age as int : age() as int, ); }
//$Student|[]|[rollNo:String:$Student, name:String:$Human, age:int:$Human]$Human|[]|[name:String:null, age:int:null] //$Student|[]|[rollNo:String:$Student, name:String:$Human, age:int:$Human] factory Human.fromJson(Map<String, dynamic> json) { if (json['className'] == "Student") { return _$StudentFromJson( json, ); } else if (json['className'] == "Human") { return _$HumanFromJson( json, ); } else { throw UnsupportedError( "The className '${json['className']}' is not supported by the Human.fromJson constructor."); } }
// ignore: unused_field Map<Type, Object? Function(Never)> _fns = {};
Map<String, dynamic> toJson_2([Map<Type, Object? Function(Never)>? fns]) { this._fns = fns ?? {}; return toJson(); }
Map<String, dynamic> toJson() { final Map<String, dynamic> data = _$HumanToJson( this, ); // Adding custom key-value pair data['className'] = 'Human';
} }
extension $Human_changeTo_E on $Human { Human changeToHuman({ String Function()? name, int Function()? age, }) { return Human.( name: name == null ? this.name as String : name() as String, age: age == null ? this.age as int : age() as int, ); }
Student changeToStudent({ required String rollNo, String Function()? name, int Function()? age, }) { return Student.( rollNo: rollNo as String, name: name == null ? this.name as String : name() as String, age: age == null ? this.age as int : age() as int, ); } }
enum Human$ { name, age }
part of 'human.dart';
// ** // JsonSerializableGenerator // **
Human _$HumanFromJson(Map<String, dynamic> json) => Human( name: json['name'] as String, age: json['age'] as int, );
Map<String, dynamic> _$HumanToJson(Human instance) => <String, dynamic>{ 'name': instance.name, 'age': instance.age, };