fetchAllArtCrimes function

Future<List<ArtCrime>> fetchAllArtCrimes()

Fetches all art crimes.

!!! 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<ArtCrime>> fetchAllArtCrimes() async {
  final List<ArtCrime> artCrimes = [];

  // Fetch all art crimes by iterating through every page.
  for (int i = 1; i < double.infinity; i++) {
    final ArtCrimeResultSet artCrimesResultSet = await fetchArtCrimes(
      pageSize: 50,
      page: i,
    );

    // Once we reach the end of the art crimes, break out of the loop.
    if (artCrimesResultSet.total == null ||
        artCrimesResultSet.total! == 0 ||
        artCrimesResultSet.artCrimes == null ||
        artCrimesResultSet.artCrimes!.isEmpty) {
      break;
    }

    artCrimes.addAll(artCrimesResultSet.artCrimes!);
  }

  final List<ArtCrime> uniqueArtCrimes = [];

  // Remove duplicates.
  for (final ArtCrime artCrime in artCrimes) {
    if (!uniqueArtCrimes.contains(artCrime)) {
      uniqueArtCrimes.add(artCrime);
    }
  }

  return uniqueArtCrimes;
}