saveCountries method

Future<void> saveCountries(
  1. Map<String, Country> countries
)

Save countries to cache

Implementation

Future<void> saveCountries(Map<String, Country> countries) async {
  try {
    final prefs = await SharedPreferences.getInstance();

    final jsonList = countries.values
        .map((country) => country.toJson())
        .toList();

    await prefs.setString(_cacheKey, json.encode(jsonList));
    await prefs.setInt(_timestampKey, DateTime.now().millisecondsSinceEpoch);
  } catch (e) {
    print('Failed to save countries to cache: $e');
  }
}