Skip to content

[shared_preferences] Enable Safe Reuse of Prefix Settings and Instance Checks #151036

@timbrueckner

Description

@timbrueckner

Use case

As per the current implementation, the shared_preferences plugin allows setting a custom prefix using setPrefix before any instances are instantiated. However, if setPrefix is called after getInstance, it results in a failure, even if the new prefix matches the previously set one. This poses challenges in complex applications where multiple independent packages might attempt to set or check the prefix independently, leading to potential conflicts and runtime exceptions.

Proposal

  1. Introduce a hasInstance() method: This method would allow developers to check if an instance of SharedPreferences has already been created, which helps in deciding whether it’s safe to call setPrefix.
  2. Modify setPrefix() to allow idempotent operations: If setPrefix is called with a prefix that matches the already set prefix, the method should not fail. This adjustment would make the prefix setting idempotent and safer in environments where multiple checks might occur.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterp: shared_preferencesPlugin to read and write Shared Preferencespackageflutter/packages repository. See also p: labels.r: fixedIssue is closed as already fixed in a newer versionteam-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions