Dana-Ferguson / time_machine

A date and time API for Dart
https://pub.dartlang.org/packages/time_machine/
Apache License 2.0
126 stars 37 forks source link

NoSuchMethodError: The getter 'id' was called on null #17

Open SteveAlexander opened 5 years ago

SteveAlexander commented 5 years ago

This was reported via Sentry.io, with an app that is using flutter_native_timezone.

The problem is in vm.dart, line 91

    var local = timeZoneOverride != null ? await tzdb.getZoneOrNull(timeZoneOverride) : await _figureOutTimeZone(tzdb);
    // todo: cache local more directly? (this is indirect caching)
    TzdbIndex.localId = local.id;

Getting local.id fails because local is null.

I think the culprit is tzdb.getZoneOrNull that might return null.

I guess if it returns null, Perhaps timemachine should log a warning message, and then resort to _figureOutTimeZone

JonasWanke commented 4 years ago

I had the same problem when executing the following code on an android emulator:

await TimeMachine.initialize({
  'rootBundle': rootBundle,
  'timeZone': await FlutterNativeTimezone.getLocalTimezone(),
});

The exception occurred because FlutterNativeTimezone.getLocalTimezone() returned GMT, which could not be resolved by TimeMachine. To fix this, I now manually change GMT to UTC:

var timeZone = await FlutterNativeTimezone.getLocalTimezone();
if (timeZone == 'GMT') {
  timeZone = 'UTC';
}
await TimeMachine.initialize({
  'rootBundle': rootBundle,
  'timeZone': timeZone,
});