import 'package:flutter/services.dart' show AssetBundle, rootBundle; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../core/region/region_repository.dart'; import 'comune.dart'; /// Loads the bundled municipality list. class ComuniRepository { const ComuniRepository({this.bundle}); /// Overridden in tests; null means the real asset bundle. final AssetBundle? bundle; AssetBundle get _assets => bundle ?? rootBundle; static String assetPathFor(String regionId) => 'assets/regions/${regionId}_comuni.json'; Future load(String regionId) async { final source = await _assets.loadString(assetPathFor(regionId)); return ComuneIndex.parse(source); } } final comuniRepositoryProvider = Provider( (ref) => const ComuniRepository(), ); /// The searchable municipality index for the active region. /// /// Loaded once and kept: 1180 entries with a precomputed fold key is a /// negligible amount of memory next to a single radar frame, and rebuilding it /// per search would make typing feel slow for no reason. final comuneIndexProvider = FutureProvider((ref) async { final regionId = ref.watch(regionIdProvider); return ref.watch(comuniRepositoryProvider).load(regionId); });