asDate method

DateTime? asDate({
  1. String format = 'yyyy-MM-dd HH:mm:sss',
  2. String locale = 'en_US',
})

Implementation

DateTime? asDate({
  String format = 'yyyy-MM-dd HH:mm:sss',
  String locale = 'en_US',
}) {
  try {
    DateTime? date;
    if (this == 'null') {
      date = null;
    } else if (this is String) {
      if (isMillis(this as String, format)) {
        date = Timestamp.fromMillisecondsSinceEpoch(
          int.parse(
            this as String,
          ),
        ).toDate();
      } else {
        var df = DateFormat(
          format,
          locale,
        );
        date = df.parse(this as String);
      }
    } else if (this != null && this is Timestamp) {
      date = (this as Timestamp).toDate();
    } else if (this != null && this is DateTime) {
      date = this as DateTime;
    } else if (this != null && (this as dynamic)['_seconds'] != null) {
      date = Timestamp((this as dynamic)['_seconds'],
              (this as dynamic)['_nanoseconds'])
          .toDate();
    }
    return date;
  } catch (e) {
    printDebug('Error building date from $this: $e');
    return null;
  }
}