loadCountries method

Future<List<Country>> loadCountries({
  1. List<String> filterCountries = const [],
})

Implementation

Future<List<Country>> loadCountries(
    {List<String> filterCountries = const []}) async {
  // Load the JSON string from the asset
  String jsonString = await rootBundle.loadString(
      'packages/advance_country_picker/assets/json/countries.json');

  // Decode the JSON string into a Dart object
  List<dynamic> jsonList = jsonDecode(jsonString);

  // Cast the dynamic list to a list of maps
  List<Map<String, dynamic>> data = jsonList.cast<Map<String, dynamic>>();

  // Convert the list of maps to a list of Country objects
  var list = data.map((e) => Country.fromJson(e)).toList();
  _countries = list;
  if (filterCountries.isNotEmpty) {
    _countries.clear();
    for (var c in filterCountries) {
      var x = list.firstWhere((e) =>
          e.name.toString().toLowerCase() == c.toLowerCase() ||
          e.countryCode.toLowerCase() == c.toLowerCase());
      _countries.add(x);
    }
  }
  return _countries;
}