Skip to content

Commit

Permalink
Automatic generation of Timestamp for Positioned.fromMap if it is not… (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
guplem committed Jan 31, 2024
1 parent 0488e01 commit 113e628
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
11 changes: 8 additions & 3 deletions geolocator_platform_interface/lib/src/models/position.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,14 @@ class Position {
'The supplied map doesn\'t contain the mandatory key `longitude`.');
}

final timestamp = DateTime.fromMillisecondsSinceEpoch(
positionMap['timestamp'].toInt(),
isUtc: true);
// Assume that the timestamp is null if the map does not contain one
dynamic timestampInMap = positionMap['timestamp'];
final timestamp = timestampInMap == null
? DateTime.now()
: DateTime.fromMillisecondsSinceEpoch(
timestampInMap.toInt(),
isUtc: true,
);

return Position(
latitude: positionMap['latitude'],
Expand Down
2 changes: 1 addition & 1 deletion geolocator_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A common platform interface for the geolocator plugin.
repository: https://github.com/baseflow/flutter-geolocator/tree/main/geolocator_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 4.2.0
version: 4.2.1

dependencies:
flutter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,15 @@ void main() {

expect(bearing, -90.0);
});

test('generate Position object from map without timestamp', () async {
final Position position = Position.fromMap({
'latitude': 0.0,
'longitude': 0.0,
});

expect(position, isNotNull);
});
});
}

Expand Down

0 comments on commit 113e628

Please sign in to comment.