initializeTimeZone function
Initialize Time Zone database.
Throws TimeZoneInitException when something is worng.
import 'package:timezone/browser.dart';
initializeTimeZone().then(() {
final detroit = getLocation('America/Detroit');
final detroitNow = TZDateTime.now(detroit);
});
Implementation
Future<void> initializeTimeZone([String path = tzDataDefaultPath]) {
return HttpRequest.request(path,
method: 'GET',
responseType: 'arraybuffer',
mimeType: 'application/octet-stream')
.then((req) {
final response = req.response;
if (response is ByteBuffer) {
initializeDatabase(response.asUint8List());
} else {
throw TimeZoneInitException(
'Invalid response type: ${response.runtimeType}');
}
}).catchError((dynamic e) {
throw TimeZoneInitException(e.toString());
}, test: (e) => e is! TimeZoneInitException);
}