Skip to content

[shared_preferences] Eager SharedPreferences fetch in LegacySharedPreferencesPlugin executes synchronous disk I/O on Android main thread during startup #195313

Closed

Description

@xRossKoh

What package does this bug report belong to?

shared_preferences

What target platforms are you seeing this bug on?

Android

Have you already upgraded your packages?

Yes

Dependency versions

pubspec.lock
[Paste file content here]

Steps to reproduce

  1. Add shared_preferences to any Flutter Android application.
  2. Cold start the application and profile startup with Perfetto or similar.
  3. Observe the RegisterFlutterPlugin step for shared preferences plugin taking a long time than other plugins

Expected results

To reduce the amount of time taken for onAttachedToEngine to complete, synchronous disk I/O should not be executed in it.

Actual results

Since synchronous disk I/O is being done in the onAttachedToEngine method of the LegacySharedPreferencesPlugin, it takes a much longer time to attach to the engine than other plugins.

Code sample

Code sample

In `packages/shared_preferences/shared_preferences_android/android/src/main/kotlin/io/flutter/plugins/sharedpreferences/LegacySharedPreferencesPlugin.kt`:
```kotlin
private fun setUp(context: Context) {
  // Synchronous disk I/O on the UI thread:
  preferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
  try {
    SharedPreferencesApiRegistrar().register(this)
  } catch (ex: Exception) {
    Log.e(TAG, "Received exception while setting up SharedPreferencesPlugin", ex)
  }
}
override fun onAttachedToEngine(binding: FlutterPluginBinding) {
  setUp(binding.applicationContext)
}

Screenshots or Videos

Image

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[Paste your output here]

Activity

  1. changed the title [-]Eager SharedPreferences initialization in LegacySharedPreferencesPlugin executes synchronous disk I/O on Android main thread during startup[/-] [+]Eager SharedPreferences fetch in LegacySharedPreferencesPlugin executes synchronous disk I/O on Android main thread during startup[/+] on Oct 26, 2026
  2. changed the title [-]Eager SharedPreferences fetch in LegacySharedPreferencesPlugin executes synchronous disk I/O on Android main thread during startup[/-] [+][shared_preferences] Eager SharedPreferences fetch in `LegacySharedPreferencesPlugin` executes synchronous disk I/O on Android main thread during startup[/+] on Oct 26, 2026
  3. added
    in triagePresently being triaged by the triage team
    platform-androidAndroid applications specifically
    c: performanceRelates to speed or footprint issues (see "perf:" labels)
    p: shared_preferencesPlugin to read and write Shared Preferences
    packageflutter/packages repository. See also p: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    team-androidOwned by Android platform team
    and removed
    in triagePresently being triaged by the triage team
    on Oct 26, 2026
  4. mbcorona commented on Oct 26, 2026

    @mbcorona
    Member

    Hi @xRossKoh, thanks for the report and PR flutter/packages#13431.

    Routing to team-android.

  5. RajKavadia commented on Oct 28, 2026

    @RajKavadia

    fwiw context.getSharedPreferences() itself kicks off the xml load on a background thread (SharedPreferencesImpl starts it in the constructor), the blocking part is the first getter that has to wait for that load. so the eager call might mostly cost the file/dir checks and the first read can still block later. would be good to see a perfetto trace that shows which one actually dominates before moving it, otherwise it just shifts the stall to the first read.

  6. jesswrd commented on Oct 28, 2026

    @jesswrd
    Contributor

    @xRossKoh From android triage: Has this issue been resolved by flutter/packages#13431?

  7. gmackall commented on Nov 3, 2026

    @gmackall
    Member

    Closing as flutter/packages#13431 marks this as fixed, please let me know if there is anything outstanding and you'd like to re-open

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: performanceRelates to speed or footprint issues (see "perf:" labels)has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: shared_preferencesPlugin to read and write Shared Preferencespackageflutter/packages repository. See also p: labels.platform-androidAndroid applications specificallyteam-androidOwned by Android platform team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions