flutter-form-builder-ecosystem / flutter_form_builder

Simple form maker for Flutter Framework
https://pub.dev/packages/flutter_form_builder
MIT License
1.49k stars 541 forks source link

[General]: Exception in the form_builder_field.dart file for a custom field #1340

Closed SonGoku85 closed 1 month ago

SonGoku85 commented 11 months ago

Is there an existing issue for this?

Package/Plugin version

9.1.1

Platforms

Flutter doctor

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.16.3, on Ubuntu 22.04.3 LTS 6.2.0-37-generic, locale de_DE.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 2023.1)
[✓] IntelliJ IDEA Community Edition (version 2023.2)
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!

Minimal code example

class FormBuilderNumericField extends FormBuilderFieldDecoration<double> {

  FormBuilderNumericField({
    super.key,
    required super.name,
    super.validator,
    super.onChanged,
    double? initialValue,
  }) : super(
    builder: (FormFieldState<double?> field) {
      final state = field as _FormBuilderNumericFieldState;

      return NumericField(
        value: initialValue,
        onChanged: (newValue) {
          if(onChanged != null) {
            onChanged(newValue);
          }
          state.didChange(newValue);
        }
      );
    }
  );

  @override
  FormBuilderFieldDecorationState<FormBuilderNumericField, double> createState() => _FormBuilderNumericFieldState();
}

class _FormBuilderNumericFieldState extends FormBuilderFieldDecorationState<FormBuilderNumericField, double> {
}

Current Behavior

exception in form_builder_field.dart: type 'String' is not a subtype of type 'double?' in type cast

Expected Behavior

I wrote my own field which gives me a double. That all works too. However, if I don't set "initialValue" (=null), I get the following exception in form_builder_field.dart: type 'String' is not a subtype of type 'double?' in type cast

This is the line:

T? get initialValue =>
      widget.initialValue ??
      (_formBuilderState?.initialValue ??
          const <String, dynamic>{})[widget.name] as T?;

Steps To Reproduce

Use the small snippet of my code.

Aditional information

No response

deandreamatias commented 2 months ago

You define double? initialValue but this parameter already exist on FormBuilderFieldDecoration. Try to use super.initialValue instead double? initialValue

github-actions[bot] commented 1 month ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 1 month ago

This issue was closed because it has been stalled for 7 days with no activity.