GeoPoint class final

A point on the earth's surface.

It cannot be persisted as a property on a realm object.

Instead, you must use a custom embedded object with the following structure:

@RealmModel(ObjectType.embeddedObject)
class _Location {
  final String type = 'Point';
  final List<double> coordinates = const [0, 0];

  // The rest of the class is just convenience methods
  double get lon => coordinates[0];
  set lon(double value) => coordinates[0] = value;

  double get lat => coordinates[1];
  set lat(double value) => coordinates[1] = value;

  GeoPoint toGeoPoint() => GeoPoint(lon: lon, lat: lat);
}

You can then use it as a property on a realm object:

@RealmModel()
class _Restaurant {
  @PrimaryKey()
  late String name;
  _Location? location;
}

For convenience add an extension method on GeoPoint:

extension on GeoPoint {
  Location toLocation() {
    return Location(coordinates: [lon, lat]);
  }
}

to easily convert between GeoPoints and Locations.

The following may also be useful:

extension on (num, num) {
  GeoPoint toGeoPoint() => GeoPoint(lon: $1.toDouble(), lat: $2.toDouble());
  Location toLocation() => toGeoPoint().toLocation();
}
Implemented types

Constructors

GeoPoint({required double lon, required double lat})
Create a point from a longitude and latgitude. lon must be between -180 and 180, and lat must be between -90 and 90.

Properties

hashCode int
The hash code for this object.
no setteroverride
lat double
final
lon double
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
override