Location constructor

Location(
  1. dynamic params
)

Implementation

Location(dynamic params) {
  final Map p = _asMap(params);

  map = params;

  // Always present in both real + dummy payloads.
  coords = Coords(p['coords']);

  // Optional objects can be missing in dummy payloads.
  battery = Battery(p['battery']);
  activity = Activity(p['activity']);

  // timestamp is required for a valid Location.  Dummy payload provides it.
  final dynamic ts = p['timestamp'];
  timestamp = (ts is String) ? ts : (ts?.toString() ?? '');

  // recorded_at may be absent in dummy payloads.
  final dynamic ra = p['recorded_at'];
  recordedAt = (ra is String) ? ra : timestamp;

  // age may be absent.
  age = _mapDouble(p, 'age', fallback: 0.0);

  // is_moving may be bool or 0/1.
  isMoving = _mapBool(p, 'is_moving', fallback: false);

  // uuid may be absent in dummy payloads.
  final dynamic u = p['uuid'];
  uuid = (u is String) ? u : (u?.toString() ?? '');

  odometer = _mapDouble(p, 'odometer', fallback: 0.0);

  sample = _mapBool(p, 'sample', fallback: false);

  final dynamic e = p['event'];
  event = (e is String) ? e : (e?.toString() ?? '');

  if (p['geofence'] != null) {
    geofence = GeofenceEvent(p['geofence']);
  }

  mock = _mapBool(p, 'mock', fallback: false);

  if (p['extras'] != null && p['extras'] is Map) {
    extras = Map.from(p['extras']);
  }
}