GeofenceEvent constructor

GeofenceEvent(
  1. Map params
)

Implementation

GeofenceEvent(Map params) {
  // Remove geofence from location to prevent recursive creation of GeofenceEvent.
  Map locationData = params['location'];
  locationData.remove("geofence");

  identifier = params['identifier'];
  action = params['action'];
  timestamp = params['timestamp'];
  location = Location(locationData);
  extras = params['extras'];

  Map geofenceData = params['geofence'];

  // Normalize numeric fields.
  final double? radius = _ensureDouble(geofenceData['radius']);
  final double? latitude = _ensureDouble(geofenceData['latitude']);
  final double? longitude = _ensureDouble(geofenceData['longitude']);

  final int? loiteringDelay = _ensureInt(geofenceData['loiteringDelay']);

  // Normalize vertices: List<List<double>>
  List<List<double>> vertices = [];
  if (geofenceData['vertices'] != null) {
    List<Object?> tmp = geofenceData['vertices'];
    tmp.forEach((vertex) {
      final coords = vertex as List;
      vertices.add(
        coords.map((e) => _ensureDouble(e) ?? 0.0).toList(),
      );
    });
  }

  geofence = Geofence(
    identifier: geofenceData['identifier'],
    radius: radius,
    latitude: latitude,
    longitude: longitude,
    notifyOnEntry: geofenceData['notifyOnEntry'],
    notifyOnExit: geofenceData['notifyOnExit'],
    notifyOnDwell: geofenceData['notifyOnDwell'],
    loiteringDelay: loiteringDelay,
    vertices: vertices,
    extras: (geofenceData['extras'] != null)
        ? geofenceData['extras'].cast<String, dynamic>()
        : {},
  );

  // 🔹 Hydrate readonly runtime fields from native payload.
  if (geofenceData['hits'] != null) {
    geofence.hits = _ensureInt(geofenceData['hits']) ?? 0;
  }
  if (geofenceData['entryState'] != null) {
    geofence.entryState = _ensureInt(geofenceData['entryState']) ?? 0;
  }
  if (geofenceData['stateUpdatedAt'] != null) {
    geofence.stateUpdatedAt = _ensureDouble(geofenceData['stateUpdatedAt']);
  }
}