country_holiday 0.0.2
country_holiday: ^0.0.2 copied to clipboard
A Dart library for accessing holiday data from Google Calendar's public holiday calendars with clean architecture.
import 'dart:io';
import 'package:country_holiday/country_holiday.dart';
import 'package:dotenv/dotenv.dart';
/// Example demonstrating the Country Holiday library usage.
///
/// SETUP:
/// 1. Copy .env.example to .env
/// 2. Add your Google Calendar API key to .env
/// 3. Run: dart run example/example.dart
void main() async {
// Load environment variables
final env = DotEnv()..load();
// Get API key from environment
final apiKey = env['GOOGLE_CALENDAR_API_KEY'];
if (apiKey == null || apiKey.isEmpty || apiKey == 'your_api_key_here') {
print('❌ Error: Google Calendar API key not configured!');
print('');
print('Please follow these steps:');
print('1. Copy .env.example to .env');
print('2. Get your API key from: https://console.cloud.google.com/');
print('3. Add your API key to the .env file');
print('4. Run this example again');
exit(1);
}
final countryHolidays = CountryHolidays(apiKey: apiKey);
print('=== Country Holiday Library - Global Coverage Demo ===\n');
// Example 1: Get all available countries
print('1. Available Countries:');
final countries = await countryHolidays.getAllCountries();
print(' Total countries: ${countries.length}');
print(' First 10 countries:');
for (var country in countries.take(10)) {
print(' - ${country.code}: ${country.name}');
}
print(' ... and ${countries.length - 10} more\n');
// Example 2: Test multiple countries from different regions
print('2. Testing Holiday Retrieval Across Regions:\n');
final testCountries = {
'Americas': ['US', 'AR', 'BR', 'MX'],
'Europe': ['ES', 'DE', 'FR', 'GB'],
'Asia': ['JP', 'CN', 'IN', 'SG'],
'Middle East': ['SA', 'AE', 'IL'],
'Africa': ['ZA', 'EG', 'KE'],
'Oceania': ['AU', 'NZ'],
};
for (var region in testCountries.entries) {
print(' ${region.key}:');
for (var countryCode in region.value) {
try {
final holidays = await countryHolidays.getHolidaysByCountry(
countryCode,
);
print(' ✅ $countryCode: ${holidays.length} holidays found');
} catch (e) {
print(' ❌ $countryCode: Error - $e');
}
}
print('');
}
// Example 3: Get detailed holidays for specific countries
print('3. Detailed Holiday Information:\n');
print(' United States Holidays:');
final usHolidays = await countryHolidays.getHolidaysByCountry('US');
for (var holiday in usHolidays.take(5)) {
print(' - ${holiday.date.month}/${holiday.date.day}: ${holiday.name}');
}
print(' ... and ${usHolidays.length - 5} more\n');
// Example 4: Check if a specific date is a holiday
print('4. Checking Specific Dates:\n');
final testDates = {
'US': DateTime(2025, 7, 4), // Independence Day
'AR': DateTime(2025, 7, 9), // Independence Day
'BR': DateTime(2025, 9, 7), // Independence Day
'MX': DateTime(2025, 9, 16), // Independence Day
};
for (var entry in testDates.entries) {
final isHoliday = await countryHolidays.isHoliday(entry.key, entry.value);
final date = '${entry.value.month}/${entry.value.day}/${entry.value.year}';
print(
' ${entry.key} - $date: ${isHoliday ? "✅ Holiday" : "❌ Not a holiday"}',
);
}
print('');
// Example 5: Get holidays on a specific date across countries
print('5. Christmas Across Different Countries:\n');
final christmas = DateTime(2025, 12, 25);
for (var countryCode in ['US', 'AR', 'BR', 'ES', 'JP']) {
final holidays = await countryHolidays.getHolidaysByDate(
countryCode,
christmas,
);
if (holidays.isNotEmpty) {
print(' $countryCode: ${holidays.first.name}');
} else {
print(' $countryCode: No holiday on this date');
}
}
print('\n=== Demo Complete ===');
print('Total countries available: ${countries.length}');
print('API: Google Calendar API v3');
}