Flutter Multiselect initialValue Not Working

Issue

I am Using Flutter Multiselect(flutter_multiselect: ^0.5.1) in my project, I have loaded API data into the multi select list it works well. using initialValue property – only works if i give direct values(hardcode) , the dynamic values are not working. even if i assign values to the variable and to initialValue also not working.

Working Scenario Code:

MultiSelect(
  titleText: "Education",
  titleTextColor: Colors.black87,
  autovalidate: false,
  initialValue: ["3", "51", "77", "50"],
  dataSource: _educations,
  textField: 'education_name',
  valueField: 'education_id',
  value: null,
  filterable: true,
  change: (values) {
    
  },
  onSaved: (value) {
    print(value);
  },
  selectIcon: Icons.arrow_downward,
)

Non Working Scenario Code:

........
List educationIDS;
........


@override
  void initState() {
    ....
    educationIDS = ["3", "51", "77", "50"];//actually i have to get this from API call but here am just given raw for testing    
  }

MultiSelect(
  titleText: "Education",
  titleTextColor: Colors.black87,
  autovalidate: false,
  initialValue: educationIDS,
  dataSource: _educations,
  textField: 'education_name',
  valueField: 'education_id',
  value: null,
  filterable: true,
  change: (values) {
    
  },
  onSaved: (value) {
    print(value);
  },
  selectIcon: Icons.arrow_downward,
)

help me to resolve this problem, thanks in Advance.

Solution

Actually i find solving of my issue in your question.

I defined my initvalue as list of strings.

List < String > initVals = ["10","14"];

another thing. if you need to get them through API you need to Future Builder. you can’t get them in initState void.

Answered By – Tamer Sy

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *