Home > Mobile >  How can I get the id of a selected item in a list using the the dropdownSearch widget in flutter?
How can I get the id of a selected item in a list using the the dropdownSearch widget in flutter?

Time:10-06

I am using the DropDownSearch widget in flutter to display a list of items. I am displaying the value "acYear" from the list but I want to be able to pass the value "id" of the selected item to the controller i.e academicYearController.text .How can I achieve this please? This is what I have tried:

child: DropdownSearch < dynamic > (
  autoValidateMode: AutovalidateMode.onUserInteraction,
  validator: (value) =>
  value == null ? "Academic Year is required " : null,
  dropdownDecoratorProps: const DropDownDecoratorProps(
      dropdownSearchDecoration: InputDecoration(
        hintText: "Select academic year",
        helperText: '',
        labelText: "Academic Year *",
        contentPadding: EdgeInsets.fromLTRB(12, 12, 0, 0),
        border: OutlineInputBorder(),
      ),
    ),
    items: dataService.academicYearList.map((AcademicYear yr) {
      return yr.accYear.toString();
    }).toList(),
    // items: dataService.academicYearList.map((AcademicYear yr) {
    //   return DropdownMenuItem(
    //     child: Text(yr.id.toString()),
    //     value: yr.id,
    //   );
    // }).toList(),
    onChanged: (value) {
      academicYearController.text = value;
      setState(() {
        dataService.selectedAcademicYear = dataService.selected;
      });
      debugPrint('selectedId: ${academicYearController.text}');
    },
),

And the AcademicYear Object and academicYear List look like this:

class AcademicYear {
  int id;
  String accYearId;
  String accYear;
  int status;

  AcademicYear({
    required this.id,
    required this.accYearId,
    required this.accYear,
    required this.status,
  });

  [{
    "id": 1,
    "accYearId": "20212022",
    "accYear": "20212022",
    "status": 1
  }]

I have Added the API call to get the academicYear data just incase am doing something wrong as shown:

Future < List < AcademicYear >> getYears() async {
  academicUrl = "${dataService.baseUrl}/academicyear";
  // debugPrint('url: $academicUrl');

  http.Response response = await http.get(
    Uri.parse(academicUrl),
    headers: {
      "Content-Type": "application/json",
    },
  );

  List < AcademicYear > result(dynamic str) => List < AcademicYear > .from(
    json.decode(str).map((x) => AcademicYear.fromJson(x)));

  // debugPrint('result: ${response.body}');

  if (response.statusCode == 200) {
    return result(response.body);
  } else {
    throw Exception('error loading object');
  }
}

CodePudding user response:

In your DropdownSearch's onChanged , do this:

onChanged: (value) {
      var xList = dataService.academicYearList.where((element) => element.accYear == value);

      if (xList.isNotEmpty) {
         AcademicYear x = xList.first;
         print("x = ${x.id}");
         academicYearController.text = value;
      }
      
      setState(() {
        dataService.selectedAcademicYear = dataService.selected;
      });
      debugPrint('selectedId: ${academicYearController.text}');
    },
  • Related