getTileBytes method

Future<Uint8List?> getTileBytes(
  1. String urlTemplate,
  2. int z,
  3. int x,
  4. int y,
)

Implementation

Future<Uint8List?> getTileBytes(String urlTemplate, int z, int x, int y) async {
  try {
    // Primero intentar desde la base de datos
    final cachedBytes = await _store.read(urlTemplate, z, x, y);
    if (cachedBytes != null && cachedBytes.isNotEmpty) {
      return cachedBytes;
    }

    // Si no está en cache, descargar
    final url = _buildUrl(urlTemplate, z, x, y);
    final response = await http.get(Uri.parse(url));

    if (response.statusCode == 200) {
      final bytes = response.bodyBytes;
      // Guardar en cache para próxima vez
      await _store.write(urlTemplate, z, x, y, Uint8List.fromList(bytes));
      return bytes;
    }
  } catch (e) {
    print('Error en TileCacheManager: $e');
  }
  return null;
}