VB10 / vexana

Vexana is network manager project with dio.
https://pub.dev/packages/vexana
MIT License
146 stars 39 forks source link

Using Freezed in the Models #103

Closed behzodfaiziev closed 1 month ago

behzodfaiziev commented 1 month ago

Until today, I have been using JsonSerializable so that toJson and fromJson could be generated automatically. I I wanted to migrate to Freezed package because it automatically generates copyWith as well

However, 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 freezed

import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'person.freezed.dart';
part 'person.g.dart';

@freezed
class Person with _$Person {
  const factory Person({
    required String firstName,
    required String lastName,
    required int age,
  }) = _Person;

  factory Person.fromJson(Map<String, Object?> json)
  => _$PersonFromJson(json);
}

Reproduced steps for the issue:

  1. extend Person with INetworkModel
    
    import 'package:flutter/foundation.dart';
    import 'package:freezed_annotation/freezed_annotation.dart';
    import 'package:vexana/vexana.dart';

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); }


2. run `dart run build_runner build`. 
  - person.freezed.dart and person.g.dart are generated
3. see the error in `person.freezed.dart: 118`
![Screenshot from 2024-07-28 23-48-14](https://github.com/user-attachments/assets/8d7a82f5-38ef-4e47-8580-63bc9d3efb48)

P.S. Initially I used `dartj.web.app` to create a model which caused this issue. I would like to ask if I am doing something wrong? Since the `person.freezed.dart` is autogenerated I can't modify it to fix this issue, because in the next `build_runner` it would be reproduced again as loop. If you have the solution for this issue, could you please ask DartJ author to update the Model generator to be updated as well
behzodfaiziev commented 1 month ago

@MehmetKaranlik I would like to ask if you came accross with this issue

MehmetKaranlik commented 1 month ago

@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.

behzodfaiziev commented 1 month ago

@MehmetKaranlik That would be great, thank you!

aligconnectinno commented 1 month ago

@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

behzodfaiziev commented 1 month ago

@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.