fetchAllWantedPersons function

Future<List<WantedPerson>> fetchAllWantedPersons()

Fetches all wanted persons.

!!! WARNING !!! This method will trigger CloudFlare to block your IP address for a few minutes. It makes too many requests too quickly. !!! WARNING !!!

Implementation

Future<List<WantedPerson>> fetchAllWantedPersons() async {
  final List<WantedPerson> wantedPersons = [];

  // Fetch all wanted persons by iterating through every page.
  for (int i = 1; i < double.infinity; i++) {
    final WantedPersonResultSet wantedPersonResultSet =
        await fetchWantedPersons(
      pageSize: 50,
      page: i,
    );

    // Once we reach the end of the wanted persons, break out of the loop.
    if (wantedPersonResultSet.total == null ||
        wantedPersonResultSet.total! == 0 ||
        wantedPersonResultSet.wantedPersons == null ||
        wantedPersonResultSet.wantedPersons!.isEmpty) {
      break;
    }

    wantedPersons.addAll(wantedPersonResultSet.wantedPersons!);
  }

  final List<WantedPerson> uniqueWantedPersons = [];

  // Remove duplicates.
  for (final WantedPerson artCrime in wantedPersons) {
    if (!uniqueWantedPersons.contains(artCrime)) {
      uniqueWantedPersons.add(artCrime);
    }
  }

  return uniqueWantedPersons;
}