Skip to content

[FormBuilderTypeAhead]: How to Select a value to be preselected  #86

@abhilashsajeev

Description

@abhilashsajeev

Is there an existing issue for this?

  • I have searched the existing issues

Package/Plugin version

Current

Platforms

  • Android
  • iOS
  • Linux
  • MacOS
  • Web
  • Windows

Flutter doctor

Flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.10.6, on macOS 13.0 22A380 darwin-arm64, locale en-IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.2)
[✓] VS Code (version 1.82.2)
[✓] Connected device (2 available)
[✓] Network resources

• No issues found!

Minimal code example

Code sample
final initialValue = selected != null
                ? data.firstWhere(
                    (element) => element.iaCaseType == selected,
                  )
                : null;
            print('IA type initial value ${initialValue?.iaTypeName}');
            return FormBuilderTypeAhead<IaTypes>(
              initialValue: initialValue,
              decoration: const InputDecoration(
                border: OutlineInputBorder(),
                labelText: 'IA Type',
                hintText: 'IA Type',
              ),
              name: 'ia_type',
              itemBuilder: (context, item) {
                return ListTile(title: Text(item.iaTypeName!));
              },
              selectionToTextTransformer: (suggestion) =>
                  suggestion.iaTypeName!,
              controller: TextEditingController(text: ''),
              validator: FormBuilderValidators.compose(
                  [FormBuilderValidators.required()]),
              suggestionsCallback: (query) {
                final pattern = RegExp('[^a-zA-Z0-9\s]');
                if (query.isNotEmpty) {
                  var lowercaseQuery = query.toLowerCase();
                  return data.where((IaTypes item) {
                    return item.iaTypeName!
                        .toLowerCase()
                        .replaceAll(pattern, '')
                        .contains(lowercaseQuery);
                  }).toList(growable: false)
                    ..sort((a, b) => a.iaTypeName!
                        .toLowerCase()
                        .indexOf(lowercaseQuery)
                        .compareTo(b.iaTypeName!
                            .toLowerCase()
                            .indexOf(lowercaseQuery)));
                } else {
                  return data;
                }
              },
            );

Current Behavior

No preselected value is showing. Even though I have selected initialValue and passed it to intialvalue field in the FormBuilderTypeAhead

Expected Behavior

when initialvalue to be passed to filed it should show it as preseleted item

Steps To Reproduce

Using Obx of Getx

Aditional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions