-
Notifications
You must be signed in to change notification settings - Fork 29
Description
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 YourFormNameFormThis 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:
- Define a form model using
freezedand 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); }
- Generate code using
reactive_forms_generator: ^8.0.0. - 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.0reactive_forms_annotations: ^8.0.0reactive_forms_generator: ^8.0.0freezed: ^3.0.6freezed_annotation: ^3.0.0json_annotation: ^4.9.0json_serializable: ^6.9.5build_runner: ^2.4.15