'What is the right way of Implementing FormBuilderChoiceChip by using flutter_form_builder package?

  1. on select of particular chip I want to use the value for some calculation and display. I tried to achieve it with onChanged method and it is not working, I am getting the following error when I tried to implement it.

Couldn't infer type parameter 'T'. Tried to infer 'int?' for 'T' which doesn't work: Parameter 'onChanged' declared as 'void Function(T?)?' but argument is 'void Function(Object?)'. The type 'int?' was inferred from: Parameter 'options' declared as 'List' but argument is 'List<FormBuilderFieldOption<int?>>'. Consider passing explicit type argument(s) to the generic.

FormBuilderChoiceChip(
              name: 'choice_chip',
              decoration: InputDecoration(
                labelText: 'Select an option',
              ),
              onChanged: (value){
                  print(value);   
              }
              options: [
                FormBuilderFieldOption(
                    value: 'Test', child: Text('Test')),
                FormBuilderFieldOption(
                    value: 'Test 1', child: Text('Test 1')),
                FormBuilderFieldOption(
                    value: 'Test 2', child: Text('Test 2')),
                FormBuilderFieldOption(
                    value: 'Test 3', child: Text('Test 3')),
                FormBuilderFieldOption(
                    value: 'Test 4', child: Text('Test 4')),
              ],
            )

Please suggest how to get the selected chip value.

  1. On-screen load I want to make one specific chip pre-selected how to achieve it?

I tried with initialValue = 2 and it is not working.



Solution 1:[1]

The solution to both of your problems. Try this snippet:

Widget getType() {
return FormBuilderChoiceChip<dynamic>(
  name: 'Type',
  initialValue: 8,
  options: const [
    FormBuilderFieldOption(value: 8),
    FormBuilderFieldOption(value: 4),
    FormBuilderFieldOption(value: 15),
  ],
  onChanged: (value) {
    print(value);
  },
);

}

Solution 2:[2]

I got answer for my second question....

it was my fault... I assumed that initialvalue is an index of options... its actually a one of the option value which you want to be pre-selected ....

Example code:

 FormBuilderChoiceChip<int>(
  name: 'test_name',
  initialValue: 8,
  options: const [
    FormBuilderFieldOption(value: 8),
    FormBuilderFieldOption(value: 4),
    FormBuilderFieldOption(value: 15),
  ],
}

more details

however still need answer for 1st question...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arpan Shingala
Solution 2