artflutter / reactive_forms_generator

Other
89 stars 22 forks source link

How to use with freezed unions? #103

Open MiniSuperDev opened 1 year ago

MiniSuperDev commented 1 year ago

Hello,, when use freezed it generate only private classes and also expect other type in the model, for example instead of Union1 it expect _$Union1

Thank you

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

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

@freezed
class FreezedUnion with _$FreezedUnion {
  @ReactiveFormAnnotation()
  const factory FreezedUnion.union1({
    @FormControlAnnotation() required String text,
  }) = Union1;

  @ReactiveFormAnnotation()
  const factory FreezedUnion.union2({
    @FormControlAnnotation() required int number,
  }) = Union2;

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

void main() {
  final form = _$Union1FormBuilder( // It should be public
    model: _$Union1(text: 'foo'), // It should be Union1
    builder: (_, __, ___) {
      return Container();
    },
  );
}
  reactive_forms_generator: ^1.0.1
vasilich6107 commented 1 year ago

Hi @MiniSuperDev Could you clarify the usecase for unions in describing form models?

MiniSuperDev commented 1 year ago

Hi @vasilich6107 At the moment I have a notion type app, in which blocks of different types can be created. In this case each type of block is a union class.

MiniSuperDev commented 1 year ago

@vasilich6107 In freezed for example to make json_serializable works with union, each union class should have its own annotations

vasilich6107 commented 2 weeks ago

Hey @MiniSuperDev I probably realized the source of issue Will try to fix some day