fromMap static method

Position fromMap(
  1. dynamic message
)

Converts the supplied Map to an instance of the Position class.

Implementation

static Position fromMap(dynamic message) {
  final Map<dynamic, dynamic> positionMap = message;

  if (!positionMap.containsKey('latitude')) {
    throw ArgumentError.value(positionMap, 'positionMap',
        'The supplied map doesn\'t contain the mandatory key `latitude`.');
  }

  if (!positionMap.containsKey('longitude')) {
    throw ArgumentError.value(positionMap, 'positionMap',
        'The supplied map doesn\'t contain the mandatory key `longitude`.');
  }

  // Assume that the timestamp is null if the map does not contain one
  dynamic timestampInMap = positionMap['timestamp'];
  final timestamp = timestampInMap == null
      ? DateTime.now()
      : DateTime.fromMillisecondsSinceEpoch(
          timestampInMap.toInt(),
          isUtc: true,
        );

  return Position(
    latitude: positionMap['latitude'],
    longitude: positionMap['longitude'],
    timestamp: timestamp,
    altitude: _toDouble(positionMap['altitude']),
    altitudeAccuracy: _toDouble(positionMap['altitude_accuracy']),
    accuracy: _toDouble(positionMap['accuracy']),
    heading: _toDouble(positionMap['heading']),
    headingAccuracy: _toDouble(positionMap['heading_accuracy']),
    floor: positionMap['floor'],
    speed: _toDouble(positionMap['speed']),
    speedAccuracy: _toDouble(positionMap['speed_accuracy']),
    isMocked: positionMap['is_mocked'] ?? false,
    // A platform that could not measure a value omits its key — see
    // `LocationMapper.toHashMap` on Android, which guards every optional
    // field with the platform's own predicate. `_toDouble` substitutes 0.0
    // for the missing value; these flags record that nothing measured it.
    //
    // `toJson` always writes the numeric keys, so on a round trip key
    // presence alone would report a measurement that never happened. The
    // explicit `has_*` key therefore wins when it is present.
    hasAccuracy: _presence(positionMap, 'has_accuracy', 'accuracy'),
    hasAltitude: _presence(positionMap, 'has_altitude', 'altitude'),
    hasAltitudeAccuracy:
        _presence(positionMap, 'has_altitude_accuracy', 'altitude_accuracy'),
    hasHeading: _presence(positionMap, 'has_heading', 'heading'),
    hasHeadingAccuracy:
        _presence(positionMap, 'has_heading_accuracy', 'heading_accuracy'),
    hasSpeed: _presence(positionMap, 'has_speed', 'speed'),
    hasSpeedAccuracy:
        _presence(positionMap, 'has_speed_accuracy', 'speed_accuracy'),
  );
}