Skip to content

Commit 113e628

Browse files
authored
Automatic generation of Timestamp for Positioned.fromMap if it is not… (#1411)
* Automatic generation of Timestamp for Positioned.fromMap if it is not existent * Added test - does not pass * Moved test to the proper location * Update geolocator_platform_interface_test.dart
1 parent 0488e01 commit 113e628

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

geolocator_platform_interface/lib/src/models/position.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,14 @@ class Position {
141141
'The supplied map doesn\'t contain the mandatory key `longitude`.');
142142
}
143143

144-
final timestamp = DateTime.fromMillisecondsSinceEpoch(
145-
positionMap['timestamp'].toInt(),
146-
isUtc: true);
144+
// Assume that the timestamp is null if the map does not contain one
145+
dynamic timestampInMap = positionMap['timestamp'];
146+
final timestamp = timestampInMap == null
147+
? DateTime.now()
148+
: DateTime.fromMillisecondsSinceEpoch(
149+
timestampInMap.toInt(),
150+
isUtc: true,
151+
);
147152

148153
return Position(
149154
latitude: positionMap['latitude'],

geolocator_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: A common platform interface for the geolocator plugin.
33
repository: https://github.com/baseflow/flutter-geolocator/tree/main/geolocator_platform_interface
44
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
55
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
6-
version: 4.2.0
6+
version: 4.2.1
77

88
dependencies:
99
flutter:

geolocator_platform_interface/test/geolocator_platform_interface_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,15 @@ void main() {
324324

325325
expect(bearing, -90.0);
326326
});
327+
328+
test('generate Position object from map without timestamp', () async {
329+
final Position position = Position.fromMap({
330+
'latitude': 0.0,
331+
'longitude': 0.0,
332+
});
333+
334+
expect(position, isNotNull);
335+
});
327336
});
328337
}
329338

0 commit comments

Comments
 (0)