diff --git a/lib/src/location_database.dart b/lib/src/location_database.dart index 812564e..d3c0151 100644 --- a/lib/src/location_database.dart +++ b/lib/src/location_database.dart @@ -28,6 +28,13 @@ class LocationDatabase { /// Finds [Location] by its name. Location get(String name) { + if (!isInitialized) { + // Before you can get a location, you need to manually initialize the + // timezone location database by calling initializeDatabase or similar. + throw LocationNotFoundException( + 'Tried to get location before initializing timezone database'); + } + final loc = _locations[name]; if (loc == null) { throw LocationNotFoundException( diff --git a/test/datetime_test.dart b/test/datetime_test.dart index 032b29d..d705282 100644 --- a/test/datetime_test.dart +++ b/test/datetime_test.dart @@ -154,5 +154,14 @@ Future main() async { }); }); }); + }); + + group('Timezones', () { + test( + 'getLocation throws $LocationNotFoundException for unrecognized timezone', + () { + expect(() => getLocation('non-existent-location'), + throwsA(TypeMatcher())); + }); }); } diff --git a/test/datetime_test_no_database.dart b/test/datetime_test_no_database.dart index a2bb837..4323083 100644 --- a/test/datetime_test_no_database.dart +++ b/test/datetime_test_no_database.dart @@ -1,11 +1,21 @@ @TestOn('vm') import 'package:test/test.dart'; import 'package:timezone/standalone.dart'; +import 'package:timezone/timezone.dart'; void main() { group('Without initializing timezone database', () { test('Can still construct TZDateTime.utc', () { TZDateTime.utc(2019); }); + + test( + 'getLocation throws $LocationNotFoundException with message about the database not being initialized', + () { + expect( + () => getLocation('America/New_York'), + throwsA(TypeMatcher() + .having((e) => e.msg, 'msg', contains('database')))); + }); }); }