tileImage method

Image? tileImage({
  1. int? randomRange,
  2. double? size,
})

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

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.

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

Implementation

Image? tileImage({
  int? randomRange,
  double? size,
}) {
  final int storeLen = _storeDirectory.stats.storeLength;
  if (storeLen == 0) return null;

  int i = 0;

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

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

  return null;
}