getStateDropDownList method

Future<void> getStateDropDownList()

Implementation

Future<void> getStateDropDownList() async {
  try {
    final response = await apiService.getData(
      AppUrls.getStateListForDropDown,
      queryParams: {'countryId': 101},
    );

    if (response.statusCode == 200 && response.data != null) {
      final list =
          DropDownResponse.fromDecodedJsonList(response.data['data']);

      stateDropDownList.assignAll(list);

      // ✅ FIX: Re-assign selected state from list
      if (selectedState.value != null) {
        final match = list.firstWhereOrNull(
          (e) => e.value == selectedState.value!.value,
        );

        if (match != null) {
          selectedState.value = match;
        }
      }
    }
  } catch (e) {
    debugPrint('Failed to fetch state dropdown: $e');
  }
}