flutter_form_builder icon indicating copy to clipboard operation
flutter_form_builder copied to clipboard

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

Open SonGoku85 opened this issue 2 years ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Package/Plugin version

9.1.1

Platforms

  • [X] Android
  • [ ] iOS
  • [ ] Linux
  • [ ] MacOS
  • [ ] Web
  • [ ] Windows

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

SonGoku85 avatar Dec 12 '23 12:12 SonGoku85