I wanted to be able to compare two DateTimes in dart with simple operators so I created a file called 'date_comparison.dart' containing the following code:
extension DateComparison on DateTime {
bool operator >(other) => compareTo(other) > 0;
bool operator <(other) => compareTo(other) < 0;
bool operator >=(other) => compareTo(other) >= 0;
bool operator <=(other) => compareTo(other) <= 0;
}
In another file I import this file and use the comparison operators and VS Code doesn't give me any errors. (The code of that file is a bit too much to post it here but the point is that everything works apart from that)
However, when I try to run the app I get a runtime error NoSuchMethodError
saying that »Class 'DateTime' has no instance method '<='.«
So I fixed the problem:
Turns out that whenever a variable of which the type is modified by an extension is of type dynamic, even when a correct value is assigned to that variable, the extension gets ignored.
For my part, I accidentally forgot to declare the DateTime type on a variable on which I wanted to use my operator extension so it was dynamic and the extension had no effect.