Abel Herlambang
Abel Herlambang

Reputation: 75

Unhandled Exception: Invalid argument (scheduledDate): Must be a date in the future: Instance of 'TZDateTime'

This is me trying to create a daily scheduled notification using flutter_local_notification. I've tried changing the time and uses different methods such as using TZDateTime.from instead of TZDateTime.utc and yet it still doesn't fix anything. Here's the code:


        import 'package:timezone/data/latest.dart' as tz;
        import 'package:timezone/timezone.dart' as tz;

        \\\

        const platform = const MethodChannel('helper');

        tz.initializeTimeZones();
        final String timeZoneName = 'Asia/Bangkok';
        tz.setLocalLocation(tz.getLocation(timeZoneName));

        final tzTime = tz.TZDateTime.utc(time.year, time.month, time.day, i, time.minute + 1, time.second);

        await flutterLocalNotificationsPlugin.zonedSchedule(
            0,
            'Shouter Clock',
            "It's $dateTime",
            tzTime,
            platformChannelSpecifics,
            payload: "Test",
          androidAllowWhileIdle: true,
          uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
        );

Upvotes: 2

Views: 4183

Answers (1)

Superpace
Superpace

Reputation: 43

In my opinion , It's throwing this error because of the time which you selected is before than now . In their pub.dev page , there is an example for solve this problem .

tz.TZDateTime _nextInstanceOfTenAM() {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduledDate =
    tz.TZDateTime(tz.local, now.year, now.month, now.day, 10);
if (scheduledDate.isBefore(now)) {
  scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}

It's the example link from their pub.dev page

Upvotes: 4

Related Questions