Skip to content

reactive_forms_generator 8.0.0: Generated FormArrayBuilder code causes build error due to nullability mismatch with FormModel.of(context) when using freezedΒ #193

@smithemely

Description

@smithemely

After upgrading reactive_forms_generator to 8.0.0, our application, which uses reactive_forms in conjunction with freezed for model definitions, fails to build. The error originates in the generated Reactive<YourFormName>FormArrayBuilder (e.g., ReactiveTestFormFormArrayBuilder).

The problematic line in the generated code is:

formArray: formControl ?? control?.call(ReactiveTestFormForm.of(context)),

The issue is that ReactiveTestFormForm.of(context) (or Reactive<YourFormName>Form.of(context)) can return null:

static TestFormForm? of( // Or YourFormNameForm?
    BuildContext context, {
    bool listen = true,
  }) {
    // ... implementation returns TestFormForm?
  }

However, the control callback in the ReactiveTestFormFormArrayBuilder expects a non-nullable formModel:

final FormArray<ReactiveTestFormFormArrayBuilderT>? Function(
      TestFormForm formModel)? control; // Or YourFormNameForm

This leads to a compile-time error because a potentially null value from .of(context) is passed to the control callback which expects a non-null argument.

To Reproduce:

  1. Define a form model using freezed and annotate it with @Rf().
    Example model structure:
    import 'package:freezed_annotation/freezed_annotation.dart';
    import 'package:reactive_forms_annotations/reactive_forms_annotations.dart';
    
    part 'test_form.freezed.dart';
    part 'test_form.g.dart';
    part 'test_form.gform.dart';
    
    @freezed
    @Rf()
    sealed class TestForm with _$TestForm {
      const factory TestForm({
        @RfControl() required String someField,
      }) = _TestForm;
    
      factory TestForm.fromJson(Map<String, dynamic> json) =>
          _$TestFormFromJson(json);
    }
  2. Generate code using reactive_forms_generator: ^8.0.0.
  3. Attempt to build the application.

Expected Behavior:
The generated code should correctly handle the potential nullability of FormModel.of(context) to prevent build errors, perhaps by adding a null check or an assertion that FormModel.of(context) is not null in this specific context if it's guaranteed.

Affected Generated Code Snippet (example for a form named TestForm):

class ReactiveTestFormFormArrayBuilder<ReactiveTestFormFormArrayBuilderT> // Suffix might vary based on array property name
    extends StatelessWidget {
  // ...
  final FormArray<ReactiveTestFormFormArrayBuilderT>? Function(
      TestFormForm formModel)? control; // Expects non-nullable TestFormForm
  // ...
  @override
  Widget build(BuildContext context) {
    return ReactiveFormArray<ReactiveTestFormFormArrayBuilderT>(
      formArray: formControl ?? control?.call(ReactiveTestFormForm.of(context)), // ReactiveTestFormForm.of(context) can be null
      // ...
    );
  }
}

Environment:

  • Flutter: 3.32.2
  • Dart: 3.8.1

Package Versions:

  • reactive_forms: ^18.0.0
  • reactive_forms_annotations: ^8.0.0
  • reactive_forms_generator: ^8.0.0
  • freezed: ^3.0.6
  • freezed_annotation: ^3.0.0
  • json_annotation: ^4.9.0
  • json_serializable: ^6.9.5
  • build_runner: ^2.4.15

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions