Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime comparison with TZDateTime throws NoSuchMethodError #10

Closed
jkokkala opened this issue Jan 26, 2018 · 4 comments
Closed

DateTime comparison with TZDateTime throws NoSuchMethodError #10

jkokkala opened this issue Jan 26, 2018 · 4 comments
Labels
blocked The issue is blocked by another issue

Comments

@jkokkala
Copy link

The following line of code will cause a runtime error:
new DateTime.now().isAfter(new TZDateTime.now(UTC))

The error:

EXCEPTION: NoSuchMethodError: method not found: ''
Receiver: null
Arguments: []
STACKTRACE: 
dart:core                                                    isAfter

This happens because DateTime.isAfter is using a private value _value to do the comparison but since TZDateTime only implements the interface, it has no privates that would be accessible. As mentioned in dart-lang/sdk#25462 there's not much this library can do about it since it's a language level problem which won't be caught by any compiler.

@srawlins
Copy link
Owner

srawlins commented Feb 5, 2018

More specifically, this is blocked by dart-lang/sdk#32051.

@sir-boformer
Copy link

this is still an issue

@srawlins srawlins added the blocked The issue is blocked by another issue label Sep 9, 2019
@NightOwlCoder
Copy link

While the final fix from DART does not come, I've created this extension to help:

extension DateTimeExtensions on DateTime {
  DateTime clone({
    int year: null,
    int month: null,
    int day: null,
    int hour: null,
    int minute: null,
    int second: null,
    int millisecond: null,
    bool isUtc: null,
  }) {
    if (isUtc == null ? this.isUtc : isUtc) {
      return DateTime.utc(
        year == null ? this.year : year,
        month == null ? this.month : month,
        day == null ? this.day : day,
        hour == null ? this.hour : hour,
        minute == null ? this.minute : minute,
        second == null ? this.second : second,
        millisecond == null ? this.millisecond : millisecond,
      );
    }
    return DateTime(
      year == null ? this.year : year,
      month == null ? this.month : month,
      day == null ? this.day : day,
      hour == null ? this.hour : hour,
      minute == null ? this.minute : minute,
      second == null ? this.second : second,
      millisecond == null ? this.millisecond : millisecond,
    );
  }
}

PS it works perfect to work around this bug, I changed the TZDateTime to a DateTime value on the last mile of my method...

@srawlins
Copy link
Owner

dart-lang/sdk#32051 is closed as fixed, and I do not see a stack trace any longer. I believe this is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked The issue is blocked by another issue
Projects
None yet
Development

No branches or pull requests

4 participants