Skip to content

Commit 312f23f

Browse files
authored
[shared_preferences] Allow reading int as long in SharedPreferences #165781 (#9032)
Resolves issue [#165781](flutter/flutter#165781) by falling back to reading preference as an int then upcasted to a Long. ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 2405f6a commit 312f23f

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

packages/shared_preferences/shared_preferences_android/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.4.9
2+
3+
* Enables callers to use `getInt` to read preference of type `int` that was written to shared preferences by native code without passing though plugin code.
4+
15
## 2.4.8
26

37
* Updates compileSdk 34 to flutter.compileSdkVersion.

packages/shared_preferences/shared_preferences_android/android/src/main/kotlin/io/flutter/plugins/sharedpreferences/SharedPreferencesPlugin.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import io.flutter.plugin.common.BinaryMessenger
2323
import java.io.ByteArrayInputStream
2424
import java.io.ByteArrayOutputStream
2525
import java.io.ObjectOutputStream
26+
import java.lang.ClassCastException
2627
import kotlinx.coroutines.flow.Flow
2728
import kotlinx.coroutines.flow.firstOrNull
2829
import kotlinx.coroutines.flow.map
@@ -379,7 +380,12 @@ class SharedPreferencesBackend(
379380
override fun getInt(key: String, options: SharedPreferencesPigeonOptions): Long? {
380381
val preferences = createSharedPreferences(options)
381382
return if (preferences.contains(key)) {
382-
preferences.getLong(key, 0)
383+
try {
384+
preferences.getLong(key, 0)
385+
} catch (e: ClassCastException) {
386+
// Retry with getInt in case the preference was written by native code directly.
387+
preferences.getInt(key, 0).toLong()
388+
}
383389
} else {
384390
null
385391
}

packages/shared_preferences/shared_preferences_android/example/android/app/src/main/java/dev/flutter/plugins/shared_preferences_example/MainActivity.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
1717
SharedPreferences preferences =
1818
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
1919

20-
// This call adds a preference for later testing in the Dart integration tests.
20+
// These calls add preferences for later testing in the Dart integration tests.
2121
preferences
2222
.edit()
23+
.putInt("thisIntIsWrittenInTheExampleAppJavaCode", 5)
2324
.putString("thisStringIsWrittenInTheExampleAppJavaCode", "testString")
2425
.commit();
2526
}

packages/shared_preferences/shared_preferences_android/example/integration_test/shared_preferences_test.dart

+12
Original file line numberDiff line numberDiff line change
@@ -914,4 +914,16 @@ void main() {
914914
'thisStringIsWrittenInTheExampleAppJavaCode', options),
915915
'testString');
916916
});
917+
918+
testWidgets('Shared Preferences can read ints by conversion to long',
919+
(WidgetTester _) async {
920+
final SharedPreferencesAsyncAndroidOptions options =
921+
getOptions(useDataStore: false);
922+
final SharedPreferencesAsyncPlatform preferences = getPreferences();
923+
924+
expect(
925+
await preferences.getInt(
926+
'thisIntIsWrittenInTheExampleAppJavaCode', options),
927+
5);
928+
});
917929
}

packages/shared_preferences/shared_preferences_android/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: shared_preferences_android
22
description: Android implementation of the shared_preferences plugin
33
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
5-
version: 2.4.8
5+
version: 2.4.9
66

77
environment:
88
sdk: ^3.6.0

0 commit comments

Comments
 (0)