generateRandomCountries method

List<Country> generateRandomCountries(
  1. int lengthCountries
)

Implementation

List<Country> generateRandomCountries(int lengthCountries) {
  final List<Country> randomCountries = [];

  if (lengthCountries > countries.length || lengthCountries == 0) {
    throw Exception('Count exceeds the number of available countries.');
  }

  while (randomCountries.length < lengthCountries) {
    final randomIndex = _random.nextInt(countries.length);
    if (randomCountries
        .where((element) =>
            element.name == Country.fromJson(countries[randomIndex]).name)
        .isEmpty) {
      randomCountries.add(Country.fromJson(countries[randomIndex]));
    }
  }
  return randomCountries;
}