diff --git a/CHANGELOG.md b/CHANGELOG.md index 4495d9378e..3710838e68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Fix battery level convertion for iOS 16.4 ([#1424](https://github.com/getsentry/sentry-dart/pull/1424)) + ## 7.5.2 ### Fixes diff --git a/dart/lib/src/protocol/sentry_device.dart b/dart/lib/src/protocol/sentry_device.dart index e85e599aac..188794010b 100644 --- a/dart/lib/src/protocol/sentry_device.dart +++ b/dart/lib/src/protocol/sentry_device.dart @@ -178,7 +178,9 @@ class SentryDevice { model: data['model'], modelId: data['model_id'], arch: data['arch'], - batteryLevel: data['battery_level'], + batteryLevel: + (data['battery_level'] is num ? data['battery_level'] as num : null) + ?.toDouble(), orientation: data['orientation'] == 'portrait' ? SentryOrientation.portrait : data['orientation'] == 'landscape' diff --git a/dart/test/protocol/sentry_device_test.dart b/dart/test/protocol/sentry_device_test.dart index 77d41655e0..d7ed3e518f 100644 --- a/dart/test/protocol/sentry_device_test.dart +++ b/dart/test/protocol/sentry_device_test.dart @@ -115,6 +115,39 @@ void main() { true, ); }); + + test('batery level converts int to double', () { + final map = {'battery_level': 1}; + + final sentryDevice = SentryDevice.fromJson(map); + + expect( + sentryDevice.batteryLevel, + 1.0, + ); + }); + + test('batery level maps double', () { + final map = {'battery_level': 1.0}; + + final sentryDevice = SentryDevice.fromJson(map); + + expect( + sentryDevice.batteryLevel, + 1.0, + ); + }); + + test('batery level ignores if not a num', () { + final map = {'battery_level': 'abc'}; + + final sentryDevice = SentryDevice.fromJson(map); + + expect( + sentryDevice.batteryLevel, + null, + ); + }); }); group('copyWith', () {