From 2b7cb0888cd725dc69e409590861fe8118058c4d Mon Sep 17 00:00:00 2001 From: Bhaswanth Isani <99379218+bhaswanth-isani@users.noreply.github.com> Date: Wed, 18 Dec 2024 12:05:52 +0530 Subject: [PATCH] fix(device_info_plus): device memory null error on Safari and Firefox (#3401) Co-authored-by: Miguel Beltran --- .../lib/src/device_info_plus_web.dart | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/device_info_plus/device_info_plus/lib/src/device_info_plus_web.dart b/packages/device_info_plus/device_info_plus/lib/src/device_info_plus_web.dart index 1112acbbe6..30f5727ca3 100644 --- a/packages/device_info_plus/device_info_plus/lib/src/device_info_plus_web.dart +++ b/packages/device_info_plus/device_info_plus/lib/src/device_info_plus_web.dart @@ -29,7 +29,7 @@ class DeviceInfoPlusWebPlugin extends DeviceInfoPlatform { 'appCodeName': _navigator.appCodeName, 'appName': _navigator.appName, 'appVersion': _navigator.appVersion, - 'deviceMemory': _navigator.deviceMemory, + 'deviceMemory': _navigator.safeDeviceMemory, 'language': _navigator.language, 'languages': _navigator.languages.toDart, 'platform': _navigator.platform, @@ -45,3 +45,16 @@ class DeviceInfoPlusWebPlugin extends DeviceInfoPlatform { ); } } + +/// Some Navigator properties are not fully supported in all browsers. +/// However, package:web does not provide a safe way to access these properties, +/// and assumes they are always not null. +/// +/// This extension provides a safe way to access these properties. +/// +/// See: https://github.com/dart-lang/web/issues/326 +/// https://github.com/fluttercommunity/plus_plugins/issues/3391 +extension SafeNavigationGetterExtensions on html.Navigator { + @JS('deviceMemory') + external double? get safeDeviceMemory; +}