-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathlocation.dart
More file actions
91 lines (76 loc) · 2.51 KB
/
location.dart
File metadata and controls
91 lines (76 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import 'package:meta/meta.dart';
/// Contains detailed location information.
@immutable
class Location {
/// Constructs an instance with the given values for testing. [Location]
/// instances constructed this way won't actually reflect any real information
/// from the platform, just whatever was passed in at construction time.
const Location({
required this.latitude,
required this.longitude,
required this.timestamp,
});
const Location._({
required this.latitude,
required this.longitude,
required this.timestamp,
});
/// The latitude associated with the placemark.
final double latitude;
/// The longitude associated with the placemark.
final double longitude;
/// The UTC timestamp the coordinates have been requested.
final DateTime timestamp;
@override
bool operator ==(Object other) =>
other is Location &&
other.latitude == latitude &&
other.longitude == longitude &&
other.timestamp == timestamp;
@override
int get hashCode =>
latitude.hashCode ^ longitude.hashCode ^ timestamp.hashCode;
/// Converts a list of [Map] instances to a list of [Location] instances.
static List<Location> fromMaps(dynamic message) {
if (message == null) {
throw ArgumentError('The parameter \'message\' should not be null.');
}
final List<Location> list = message.map<Location>(fromMap).toList();
return list;
}
/// Converts the supplied [Map] to an instance of the [Location] class.
static Location fromMap(dynamic message) {
if (message == null) {
throw ArgumentError('The parameter \'message\' should not be null.');
}
final Map<dynamic, dynamic> locationMap = message;
final timestamp = DateTime.fromMillisecondsSinceEpoch(
locationMap['timestamp'].toInt(),
isUtc: true,
);
if (locationMap['latitude'] == null || locationMap['longitude'] == null) {
throw ArgumentError(
'The parameters latitude and longitude should not be null.',
);
}
return Location._(
latitude: locationMap['latitude'],
longitude: locationMap['longitude'],
timestamp: timestamp,
);
}
/// Converts the [Location] instance into a [Map] instance that can be
/// serialized to JSON.
Map<String, dynamic> toJson() => {
'latitude': latitude,
'longitude': longitude,
'timestamp': timestamp.millisecondsSinceEpoch,
};
@override
String toString() {
return '''
Latitude: $latitude,
Longitude: $longitude,
Timestamp: $timestamp''';
}
}