Closed behzodfaiziev closed 1 month ago
@MehmetKaranlik I would like to ask if you came accross with this issue
@behzodfaiziev Yes i did, there is an limitiation with freeze classes, I will be writing code gen specificaly for Vexana at some point i post-poned waiting for macros to be shown up on stable channel.
@MehmetKaranlik That would be great, thank you!
@behzodfaiziev Can you check this instructions. https://pub.dev/packages/freezed#adding-getters-and-methods-to-our-models
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:vexana/vexana.dart';
part 'person.freezed.dart';
part 'person.g.dart';
@freezed
class Person extends INetworkModel<Person> with _$Person {
factory Person({
required String firstName,
required String lastName,
required int age,
}) = _Person;
// Add this private constructor for custom methods
Person._();
factory Person.fromJson(Map<String, Object?> json) => _$PersonFromJson(json);
@override
Person fromJson(Map<String, dynamic> json) => Person.fromJson(json);
}
Works with private constructor
@aligconnectinno thank you for the advice, it works as expected! If you don't mind i will create PR the README.md so that users do not have similar issue in the future.
Closing as resolved.
Until today, I have been using JsonSerializable so that
toJson
andfromJson
could be generated automatically. I I wanted to migrate to Freezed package because it automatically generatescopyWith
as wellHowever, I came across with an error while generating the example model from Creating a model in freezed but adding vexana
Error
Missing concrete implementation of 'Person.fromJson'. (Documentation) Try implementing the missing method, or make the class abstract.
Here is the working autogenerated code without using
INetworkModel
from the above link of freezedReproduced steps for the issue:
part 'person.freezed.dart'; part 'person.g.dart';
@freezed /// added extends INetworkModel
class Person extends INetworkModel with _$Person {
const factory Person({
required String firstName,
required String lastName,
required int age,
}) = _Person;
factory Person.fromJson(Map<String, Object?> json) => _$PersonFromJson(json);
@override /// Also added this code as well Person fromJson(Map<String, dynamic> json) => Person.fromJson(json); }