flutter_form_builder icon indicating copy to clipboard operation
flutter_form_builder copied to clipboard

[FormBuilderFieldDecoration]: initialValue is not set to state.value

Open uzuki-P opened this issue 1 year ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Package/Plugin version

9.4.1

Platforms

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

Flutter doctor

Flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
.24.3, on Fedora Linux 40 (Workstation Edition) 6.10.12-200.fc40.x86_64, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
d)
[✓] IntelliJ IDEA Community Edition (version 2024.2)
[✓] VS Code (version 1.94.0)
[✓] Connected device (1 available)
[✓] Network resources

! Doctor found issues in 1 category.

Minimal code example

Code sample
FormBuilderFieldDecoration<ItemEntity>(
  name: fieldName,
  initialValue: initialValue,
  decoration: InputDecoration.collapsed(hintText: null),
  autovalidateMode: autovalidateMode,
  validator: FormBuilderValidators.compose([
    if (isRequired) FormBuilderValidators.required(),
    ...?validators,
  ]),
  builder: (state) {
    // this should not be needed if `initialValue` is set to `value`
    if (state.value == null && initialValue != null && state.mounted) {
      // waiting build finish then set initial value
      Future.microtask(() => state.didChange(initialValue));
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          state.value?.name ?? 'Choose_'.trlC(context),
          style: AppTextStyle.textM.setColor(
            AppColors.neutral.shade70,
          ),
        if (state.hasError)
          Text(
            state.errorText ?? '',
            style: AppTextStyle.formError,
          ).padding(top: 8),
      ],
    );
  },
);

Current Behavior

I need to set the state.value to initialValue manually and the code feels quite "hacky". Check example above.

Expected Behavior

On first render the state.value should not be null.

Steps To Reproduce

Just render the widget

Aditional information

No response

uzuki-P avatar Oct 09 '24 06:10 uzuki-P