I am trying to schedule a notification:but I got this error:
Unhandled Exception: Tried to get location before initializing timezone database
Although I initialized the Timezone in the init function:
tz.initializeTimeZones();
schedule code:
Future<void> scheduleNotifications(String title,int yy,int mm,int dd,int hh,int ii) async {
final loc_egypt = tz.getLocation('Africa/Cairo');
final tz.TZDateTime now = tz.TZDateTime.now(loc_egypt);
var schedule_30before=tz.TZDateTime(loc_egypt,yy,mm,dd,hh,ii).subtract(Duration(minutes: 30));
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
title,
"The Webinar will start after 30 minutes",
schedule_30before,
NotificationDetails(android: _androidNotificationDetails),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
}
In my case the problem was with invalid assets. I am working on a Flutter Web project, so I was doing:
import 'package:timezone/browser.dart' as tz;
void main() async {
await tz.initializeTimeZone();
// other stuff
}
After digging into the source code of timezone package, I discovered that it is initialising using a *.tzf file, which I wasn't including in my project's assets (rookie's mistake). So I edited my pubspec.yaml like this:
flutter:
assets:
- packages/timezone/data/latest.tzf
# - packages/timezone/data/latest_all.tzf <- for all the db variants
# - packages/timezone/data/latest_10y.tzf <- for 10y db variant
But there was still one more thing to do. As it turns out tz.initializeTimeZone() assumes that a default path is packages/timezone/data/latest.tzf BUT my Flutter Web project was putting all the assets in assets catalogue, so the actual path was assets/packages/timezone/data/latest.tzf and I just had to pass it as an argument to tz.initializeTimeZone('assets/packages/timezone/data/$dbVariant'). Of course you need to replace $dbVariant with the variant of your choice, the one that you included in your assets.
TL;DR
flutter:
assets:
- packages/timezone/data/latest.tzf
import 'package:timezone/browser.dart' as tz;
void main() async {
await tz.initializeTimeZone('assets/packages/timezone/data/latest.tzf');
// other stuff
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With