getDefaultCountry function Null safety

Future<Country> getDefaultCountry(
  1. BuildContext context
)

This function returns an user's current country. User's sim country code is matched with the ones in the list. If there is no sim in the device, first country in the list will be returned. This function returns an user's current country. User's sim country code is matched with the ones in the list. If there is no sim in the device, first country in the list will be returned.

Implementation

Future<Country> getDefaultCountry(BuildContext context) async {
  final list = await getCountries(context);
  try {
    final countryCode = await FlutterSimCountryCode.simCountryCode;
    if (countryCode == null) {
      return list.first;
    }
    return list.firstWhere((element) =>
        element.countryCode.toLowerCase() == countryCode.toLowerCase());
  } catch (e) {
    return list.first;
  }
}