Open markqq opened 4 years ago
Hi @markqq , I'll need a more specific example than that.
Here's an example:
import 'package:timezone/standalone.dart' as tz;
void main() async {
await tz.initializeTimeZone();
final tz.TZDateTime tzdt = tz.TZDateTime.utc(2020, 6, 15);
final DateTime dt = DateTime.now().toUtc();
print(dt.difference(tzdt));
}
This unexpectedly produces a runtime exception:
Unhandled exception:
NoSuchMethodError: Class 'TZDateTime' has no instance getter '_value'.
Receiver: Instance of 'TZDateTime'
Tried calling: _value
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 TZDateTime._value (dart:core/date_time.dart:158:13)
#2 DateTime.difference (dart:core-patch/date_patch.dart:203:54)
#3 main (file:///.../timezone_test/main.dart:8:12)
<asynchronous suspension>
#4 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
#5 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
It's because TZDateTime
doesn't have the _value
field which is part of the DateTime
interface.
It really should be caught at compile time, so it may actually be a Dart language bug. If you put skeleton DateTime
and TZDateTime
classes in the same file, the compiler does in fact complain:
class DateTime {
final int _value = 0;
}
class TZDateTime implements DateTime {
}
void main() {
}
This correctly produces a compiler exception:
Error compiling to JavaScript:
main.dart:5:7:
Error: The non-abstract class 'TZDateTime' is missing implementations for these members:
- DateTime._value
class TZDateTime implements DateTime {
^^^^^^^^^^
main.dart:2:13:
Info: 'DateTime._value' is defined here.
final int _value = 0;
^^^^^^
Error: Compilation failed.
It may be a compiler limitation that it doesn't notice the missing implementation if the classes are in separate packages. This is with Dart 2.8.4.
This issue caused a nasty runtime crash in my production app and affected a lot of users... I had to issue an emergency app store update to fix it.
It's a bug in the Dark SDK's implementation of DateTime
. I submitted a pull request that fixes it.
Any traction on this? Can still reproduce:
Hi, I can't get the difference between
DateTime
andTZDateTime
by the following code:Is there any way to solve the problem? Thx.