Skip to content

Application crashes due to Bad state: Too many elements in _getFromPhoneCode function #168

@pradipaub36

Description

@pradipaub36

When trying to get the country object using CountryParser.parsePhoneCode(1), the application crashes with the error Bad state: Too many elements.

This occurs because the _getFromPhoneCode function uses the singleWhere method, which throws an error when there are multiple matches for the condition.

Here’s the current code in question:

static Country _getFromPhoneCode(String phoneCode) {
  return Country.from(
    json: countryCodes.singleWhere(
      (Map<String, dynamic> c) => c['e164_cc'] == phoneCode,
    ),
  );
}

Error Log:

Bad state: Too many elements
#0      ListBase.singleWhere (dart:collection/list.dart:156:11)
#1      CountryParser._getFromPhoneCode (package:country_picker/src/country_parser.dart:139:26)
#2      CountryParser.parsePhoneCode (package:country_picker/src/country_parser.dart:67:12)
#3      _CoOwnerDetailsPageState.build.<anonymous closure> (package:rezio/features/home/pages/profile_tab/pages/co_owner_details_page.dart:292:59)

Any help would be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions