artflutter / reactive_forms_generator

Other
81 stars 22 forks source link

Share models between forms #152

Closed codakkk closed 3 months ago

codakkk commented 4 months ago

I'm having problems using shared models. I have an "Address" model, which is shared between all the forms that requires an address. Take as an example the form "Person" and "Garage". The problem occurrs when both forms try to use the "Address" model. This is the error: The name 'AddressForm' is defined in the libraries 'person/person.dart' and 'garage/garage.dart'. Try removing the export of one of the libraries, or explicitly hiding the name in one of the export directives.

Take as an example this snippet:

address.dart

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:reactive_forms_annotations/reactive_forms_annotations.dart';

part 'address.freezed.dart';
part 'address.g.dart';

@freezed
@RfGroup()
class Address with _$Address {
  const factory Address({
    @RfControl() String? address,
    @RfControl() String? city,
    @RfControl() String? region,
    @RfControl() String? postalCode,
    @RfControl() int? countryId,
  }) = _Address;

  factory Address.fromJson(Map<String, dynamic> json) =>
      _$AddressFromJson(json);
}

garage.dart

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:reactive_forms_annotations/reactive_forms_annotations.dart';

import '../address/address.dart';

part 'garage.freezed.dart';
part 'garage.g.dart';
part 'garage.gform.dart';

@freezed
@Rf()
class Garage with _$Garage{
  const factory Garage({
    @RfControl(validators: [RequiredValidator()]) String? name,
    @Default(Address()) Address address,
    @RfControl() String? phoneNumber,
    ......
  }) = _Garage;

  factory Garage.fromJson(Map<String, dynamic> json) => _$GarageFromJson(json);
}

person.dart

@freezed
@Rf()
class Person with _$Person{
  const factory Person({
    @RfControl(validators: [RequiredValidator()]) String? name,
    @Default(Address()) Address address,
    @RfControl() String? phoneNumber,
    ......
  }) = _Person;

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

What I do expect is that both Person and Garage share the same AddressForm class because it's a model and not an instance. Instead it's creating a new AddressForm class for each occurrence, thus creating this problem. Looking at the code of AddressForm there's no reason to have it generated inside the form file (Person or Garage in my example)

vasilich6107 commented 4 months ago

Hi @codakkk the only way is to use part files https://medium.com/@TakRutvik/part-in-dart-d78f91f66f91

check the issue https://github.com/artflutter/reactive_forms_generator/issues/86

codakkk commented 4 months ago

Hi @codakkk the only way is to use part files https://medium.com/@TakRutvik/part-in-dart-d78f91f66f91

check the issue https://github.com/artflutter/reactive_forms_generator/issues/86

Well, the problem relies on the fact that I cannot use part files like you saying, because I'm using freezed, hence part is already used. Can you provide me an example based on my code? And why is the AddressForm generated in the Person and Garage form instead of in his own file?

vasilich6107 commented 4 months ago

Try to use them. Freezed does not prevent you from using parts. You can combine the file from infinite amount of parts

codakkk commented 3 months ago

Try to use them. Freezed does not prevent you from using parts. You can combine the file from infinite amount of parts

Still this doesn't make any sense to me. Why I have to use part files when shared models could create their own classess separately?

Anyway, no, I'm not able to use part files, because it says that: Expected this library to be part of 'package:ncc/forms/guide/guide.dart', not 'package:ncc/forms/address/address.g.dart'. Try including a different part, or changing the name of the library in the part's part-of directive

vasilich6107 commented 3 months ago

Try to use them. Freezed does not prevent you from using parts. You can combine the file from infinite amount of parts

Still this doesn't make any sense to me. Why I have to use part files when shared models could create their own classess separately?

Anyway, no, I'm not able to use part files, because it says that:

`Expected this library to be part of 'package:ncc/forms/guide/guide.dart', not 'package:ncc/forms/address/address.g.dart'.

Try including a different part, or changing the name of the library in the part's part-of directive`

Could you provide a reproducible sample?

vasilich6107 commented 3 months ago

Why I have to use part files

The answer unfortunately is very simple. Cause we have no other options for now. All classes should be in one file. It could be on large file or several partitioned files.

As far as we have those options I do not plan to invest in alternative solutions

I do not even know if any other solutions are possible

codakkk commented 3 months ago

Try to use them. Freezed does not prevent you from using parts. You can combine the file from infinite amount of parts

Still this doesn't make any sense to me. Why I have to use part files when shared models could create their own classess separately? Anyway, no, I'm not able to use part files, because it says that: Expected this library to be part of 'package:ncc/forms/guide/guide.dart', not 'package:ncc/forms/address/address.g.dart'. Try including a different part, or changing the name of the library in the part's part-of directive

Could you provide a reproducible sample?

Exactly the one I wrote on the issue!

Why I have to use part files

The answer unfortunately is very simple. Cause we have no other options for now. All classes should be in one file. It could be on large file or several partitioned files.

As far as we have those options I do not plan to invest in alternative solutions

I do not even know if any other solutions are possible

Okay, makes sense. Thank you. I'll refactor my forms and make sure they're part of the same file. This seems the only way to make it work correctly

github-actions[bot] commented 3 months ago

Hi @codakkk! Your issue has been closed. If we were helpful don't forget to star the repo.

Please check our reactive_forms_widget package

We would appreciate sponsorship subscription or one time donation https://github.com/sponsors/artflutter