Flutter_form_builder: FormBuilderSearchableDropdown getting the value

Created on 14 Dec 2020  路  6Comments  路  Source: danvick/flutter_form_builder

_formKey.currentState.fields['name'].currentState.value : not working

question solved

Most helpful comment

Can't really come up with a solution, but I would suggest to try using setState() and/or Provider to change the items of the 'Subcategory' field.

UPDATE: @angwandi I put some time into it, and solidified my knowledge of states in the progress.

  1. Define a subItems List for the Subcategory field to refer to, the changeSub() function to modify subItems, and a
    searchableDropdown Widget, all before the build() Widget.
class Form extends StatefulWidget {
  @override
  _FormState createState() => _FormState();
}
class _Form extends State<DailyReport> {
  final GlobalKey<FormBuilderState> _formKey =
      GlobalKey<FormBuilderState>();
  List subItems = [];
  void changeSub(item) {
    if (item == 'Music') {
      setState(() {
        subItems = ['Pop', 'Rock', 'Blues'];
      });
    } else {
      setState(() {
        subItems = ['Fruits', 'Veg', 'Meat'];
      });
    }
  }
  Widget searchableDropdown(List items) {
    print(items);
    return FormBuilderSearchableDropdown(
        name: 'subcategory',
        items: items,
    );
  }
  @override
  Widget build(BuildContext context) {}
  1. Using the first FormBuilderSearchableDropdown labelled 'category', use the onChanged property to call changeSub(), which modifies the List that the 'subcategory' Searchable Dropdown uses.
FormBuilderSearchableDropdown(
                    name: 'category',
                    items: ['Music', 'Food'],
                    onChanged: (item) => changeSub(item)
                  ),
searchableDropdown(subItems)

Hope this is what you were looking for.

All 6 comments

Did you try this?
_formKey.currentState.value['name']

I just tried it but not achieving what I am trying to do here: Change the item list on the second formbuilder based on the value of the first one. The code below

FormBuilderSearchableDropdown(
name: 'Category',
items: EventItemsList.category,
initialValue: 'Choose',
),
FormBuilderSearchableDropdown(
initialValue: 'Music',
name: 'Subcategory',
items: _formKey.currentState.value['Category'] == 'Music'
? EventItemsList.music_sub_category
: [],
),

How about using the onChanged property to create a new list with values that match the category?

@andrewzakhartchouk example code please

Can't really come up with a solution, but I would suggest to try using setState() and/or Provider to change the items of the 'Subcategory' field.

UPDATE: @angwandi I put some time into it, and solidified my knowledge of states in the progress.

  1. Define a subItems List for the Subcategory field to refer to, the changeSub() function to modify subItems, and a
    searchableDropdown Widget, all before the build() Widget.
class Form extends StatefulWidget {
  @override
  _FormState createState() => _FormState();
}
class _Form extends State<DailyReport> {
  final GlobalKey<FormBuilderState> _formKey =
      GlobalKey<FormBuilderState>();
  List subItems = [];
  void changeSub(item) {
    if (item == 'Music') {
      setState(() {
        subItems = ['Pop', 'Rock', 'Blues'];
      });
    } else {
      setState(() {
        subItems = ['Fruits', 'Veg', 'Meat'];
      });
    }
  }
  Widget searchableDropdown(List items) {
    print(items);
    return FormBuilderSearchableDropdown(
        name: 'subcategory',
        items: items,
    );
  }
  @override
  Widget build(BuildContext context) {}
  1. Using the first FormBuilderSearchableDropdown labelled 'category', use the onChanged property to call changeSub(), which modifies the List that the 'subcategory' Searchable Dropdown uses.
FormBuilderSearchableDropdown(
                    name: 'category',
                    items: ['Music', 'Food'],
                    onChanged: (item) => changeSub(item)
                  ),
searchableDropdown(subItems)

Hope this is what you were looking for.

Thanks @andrewzakhartchouk. Works like a charm

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlmogRnD picture AlmogRnD  路  3Comments

jaxnz picture jaxnz  路  3Comments

WilliamCunhaCardoso picture WilliamCunhaCardoso  路  3Comments

droidzone picture droidzone  路  6Comments

ryanhz picture ryanhz  路  6Comments