validateLocation method

dynamic validateLocation(
  1. GeoPoint? location
)

Validates the inputted location and throws an error if it is invalid. @param location The latitude, longitude pair to be verified.

Implementation

validateLocation(GeoPoint? location) {
  String? error;

  if (location is GeoPoint == false) {
    error = 'location must be of type GeoPoint';
  } else if (location == null) {
    error = 'location cannot be null';
  } else {
    double latitude = location.latitude;
    double longitude = location.longitude;

    if (latitude is double != true || latitude.isNaN) {
      error = 'latitude must be a number';
    } else if (latitude < -90 || latitude > 90) {
      error = 'latitude must be within the range [-90, 90]';
    } else if (longitude is double != true || longitude.isNaN) {
      error = 'longitude must be a number';
    } else if (longitude < -180 || longitude > 180) {
      error = 'longitude must be within the range [-180, 180]';
    }
  }

  if (error != null) {
    throw ('Invalid GeoFire location \'' +
        location.toString() +
        '\': ' +
        error);
  }
}