Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.4.9

* 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.

## 2.4.8

* Updates compileSdk 34 to flutter.compileSdkVersion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.flutter.plugin.common.BinaryMessenger
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectOutputStream
import java.lang.ClassCastException
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
Expand Down Expand Up @@ -379,7 +380,12 @@ class SharedPreferencesBackend(
override fun getInt(key: String, options: SharedPreferencesPigeonOptions): Long? {
val preferences = createSharedPreferences(options)
return if (preferences.contains(key)) {
preferences.getLong(key, 0)
try {
preferences.getLong(key, 0)
} catch (e: ClassCastException) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider a comment documenting when we expect this to happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// Retry with getInt in case the preference was written by native code directly.
preferences.getInt(key, 0).toLong()
}
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

// This call adds a preference for later testing in the Dart integration tests.
// These calls add preferences for later testing in the Dart integration tests.
preferences
.edit()
.putInt("thisIntIsWrittenInTheExampleAppJavaCode", 5)
.putString("thisStringIsWrittenInTheExampleAppJavaCode", "testString")
.commit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,4 +914,16 @@ void main() {
'thisStringIsWrittenInTheExampleAppJavaCode', options),
'testString');
});

testWidgets('Shared Preferences can read ints by conversion to long',
(WidgetTester _) async {
final SharedPreferencesAsyncAndroidOptions options =
getOptions(useDataStore: false);
final SharedPreferencesAsyncPlatform preferences = getPreferences();

expect(
await preferences.getInt(
'thisIntIsWrittenInTheExampleAppJavaCode', options),
5);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: shared_preferences_android
description: Android implementation of the shared_preferences plugin
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.4.8
version: 2.4.9

environment:
sdk: ^3.6.0
Expand Down