tileImageAsync method

Future<Image?> tileImageAsync({
  1. int? randomRange,
  2. double? size,
})

Retrieves a tile from the store and extracts it's Image asynchronously

randomRange controls the randomness of the tile chosen (defaults to null):

  • null : no randomness - the first tile is chosen
  • value <= 0 : any tile may be chosen
  • value >= store length : any tile may be chosen
  • value < store length : any tile up to this range may be chosen, enforcing an iteration limit internally

Note that tiles are not necessarily ordered chronologically. They are usually ordered alphabetically.

Eventually returns null if there are no cached tiles in this store, otherwise an Image with size height and width.

Implementation

Future<Image?> tileImageAsync({
  int? randomRange,
  double? size,
}) async {
  final int storeLen = await _storeDirectory.stats.storeLengthAsync;
  if (storeLen == 0) return null;

  int i = 0;

  final int randomNumber = randomRange == null
      ? 0
      : Random().nextInt(
          randomRange <= 0 ? storeLen : randomRange.clamp(0, storeLen),
        );

  await for (final FileSystemEntity e
      in await _access.tiles.listWithExists()) {
    if (i >= randomNumber) {
      return Image.file(
        File(e.absolute.path),
        width: size,
        height: size,
      );
    }
    i++;
  }

  return null;
}