如何在Flutter中将DateTime转换为TZDateTime? [英] How to convert DateTime to TZDateTime in flutter?

查看:202
本文介绍了如何在Flutter中将DateTime转换为TZDateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用flutter_local_notifications软件包并通过FlutterNotificationsPlugin插入".schedule",通过我的应用程序推送本地通知.我已尝试使用".zonedSchedule"但是它需要TZDateTime值.

I am trying to push local notification through my app by using flutter_local_notifications package and as on FlutterNotificationsPlugin the ".schedule" is deprecated I tried using ".zonedSchedule" but it requires TZDateTime value.

var androidDetails = AndroidNotificationDetails(t.id, "Task Notifier",
        "Notifies if you have a task scheduled at a particular time");
    var generalNotificationDetails =
        NotificationDetails(android: androidDetails);
    var now = DateTime.now();
    var scheduledTime = DateTime(
            now.year, now.month, _dayTasks.date.day, t.time.hour, t.time.minute)
        .subtract(Duration(minutes: 5));
    //use zonedSchedule once you figure out how to convert datetime to TZdatetime
    // await notifications.zonedSchedule(
    //     0, "Task", t.description, scheduledTime, generalNotificationDetails,
    //     androidAllowWhileIdle: true,
    //     uiLocalNotificationDateInterpretation:
    //         UILocalNotificationDateInterpretation.wallClockTime);
    await notifications.schedule(0, "Ideal Day Task", t.description,
        scheduledTime, generalNotificationDetails);

推荐答案

实际上,zonedSchedule()需要TZDateTime值.我采用的方法是利用另一个包将原始DateTime值转换为TZDateTime值.以下是我使用的一种实现的摘录.看到第五行?它涉及到函数TZDateTime.from().它会使用您最初使用的DateTime并将其转换为TZDateTime.但是,它需要您当前的位置.那就是类TimeZone的所在.

Indeed, zonedSchedule() requires the TZDateTime value. The approach I took was to utilize another package to convert the original DateTime value to a TZDateTime value. Below is an excerpt of one implementation I've used. See the fifth line? It involves the function, TZDateTime.from(). It takes the DateTime you may have originally used and converts it to TZDateTime. However, it needs your current location. That's where the class, TimeZone, comes in.

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

    final timeZone = TimeZone();

    // The device's timezone.
    String timeZoneName = await timeZone.getTimeZoneName();

    // Find the 'current location'
    final location = await timeZone.getLocation(timeZoneName);

    final scheduledDate = tz.TZDateTime.from(dateTime, location);

    try {
      await _flutterLocalNotificationsPlugin.zonedSchedule(
        id,
        title,
        body,
        scheduledDate,
        platformChannelSpecifics,
        androidAllowWhileIdle: androidAllowWhileIdle,
        payload: payload,
        uiLocalNotificationDateInterpretation:
            uiLocalNotificationDateInterpretation,
      );
    } catch (ex) {
      id = -1;
    }
    return id;

我很快编写了TimeZone类.该类可与另一个插件( flutter_native_timezone )一起使用,该插件可查找设备的当前位置",并提供该语言环境的标准名称:

I quickly wrote the class, TimeZone. This class works with yet another plugin (flutter_native_timezone) that looks for the 'current location' of the device and provides the standard name for that locale:

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

class TimeZone {
  factory TimeZone() => _this ?? TimeZone._();

  TimeZone._() {
    initializeTimeZones();
  }
  static TimeZone _this;

  Future<String> getTimeZoneName() async => FlutterNativeTimezone.getLocalTimezone();

  Future<t.Location> getLocation([String timeZoneName]) async {
    if(timeZoneName == null || timeZoneName.isEmpty){
      timeZoneName = await getTimeZoneName();
    }
    return t.getLocation(timeZoneName);
  }
}

使用该课程,你很好.

这篇关于如何在Flutter中将DateTime转换为TZDateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆