Skip to content

[shared_preferences_android] Detaching a background engine unregisters the API used by the main engine #194920

Closed
flutter/packages
#13617

Description

@zkrige

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

Steps to reproduce

  1. Use shared_preferences (legacy SharedPreferences API) in an Android app that runs a second Flutter engine in the same process, for example a WorkManager task or an FCM background handler that starts its own FlutterEngine.
  2. Let the background engine start, then detach (destroy it) while the main engine is still running.
  3. Call SharedPreferences.getInstance() in the main isolate.

Expected results

The main isolate keeps working, because its engine is still attached to the plugin.

Actual results

The call throws ArgumentError: No instance with no name has been registered.

Cause: SharedPreferencesApiInstances in Messages.g.kt is a process-wide map keyed by instance name. LegacySharedPreferencesPlugin.onDetachedFromEngine calls SharedPreferencesApiRegistrar().register(null), which removes the default entry, so detaching any one engine removes the registration the other engine still uses. The error text comes from _throwNoInstanceError in messages.g.dart.

SharedPreferencesPlugin.onDetachedFromEngine does the same for the data_store entry used by SharedPreferencesAsync and SharedPreferencesWithCache.

I will open a PR for the legacy plugin.

Activity

  1. added
    platform-androidAndroid applications specifically
    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
    in triagePresently being triaged by the triage team
    on Oct 21, 2026
  2. mbcorona commented on Oct 21, 2026

    @mbcorona
    Member

    Hi @zkrige, thanks for the precise writeup and the PR.
    Routing to team-android.

  3. added a commit that references this issue on Oct 25, 2026
    2dccf71
  4. added
    P2Important issues not at the top of the work list
    triaged-androidTriaged by Android platform team
    on Oct 28, 2026
  5. stuartmorgan commented on Oct 29, 2026

    @stuartmorgan
    Contributor

    @tarrinneal I think there's a fundamental issue with the ffi/jni registration system here in multi-engine case that we never considered. With method channels, each instance of a plugin gets its own channel, because channel registration is messenger- (meaning engine-)specific, but this registration system isn't.

    The registration call is probably going to need to take some engine-specific context, handle this internally.

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

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listhas reproducible stepsThe issue has been confirmed reproducible and is ready to work onin triagePresently being triaged by the triage teamp: shared_preferencesPlugin to read and write Shared Preferencespackageflutter/packages repository. See also p: labels.platform-androidAndroid applications specificallyteam-androidOwned by Android platform teamtriaged-androidTriaged by Android platform team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions