initialize method
Initialize the manager
Implementation
Future<void> initialize({
CountryDataSource? remoteSource,
List<Country> overrides = const [],
}) async {
if (_initialized) return;
// 1. Load default countries
_countries = Map.from(defaultCountries);
// 2. Try to load from cache
final cachedCountries = await _cache.loadCountries();
if (cachedCountries != null && cachedCountries.isNotEmpty) {
_countries = cachedCountries;
}
// 3. Fetch from API if available
if (remoteSource != null) {
final apiCountries = await remoteSource.fetchCountries();
if (apiCountries != null && apiCountries.isNotEmpty) {
_countries = apiCountries;
// Save to cache for next time
await _cache.saveCountries(apiCountries);
}
}
// 4. Apply overrides
if (overrides.isNotEmpty) {
for (var country in overrides) {
_overrides[country.code] = country;
}
}
_initialized = true;
}