Skip to content

[shared_preferences] [windows,linux] Data can be corrupted due to lack of atomic file writes #89211

Open

Description

@krll-kov
 SharedPreferences _preferences = await SharedPreferences.getInstance();

This code simply fails with an error if config file is broken. Maybe it's good to clear preferences file if it contains an error. I do not know why the preferences file breaks but since i've published my app, there were some users, who reported this issue.

Platform where i faced this issue: Windows.

Flutter version: 2.2.3 from stable channel.

I have no idea why but file becomes something like this
image

Activity

  1. darshankawar commented on Aug 31, 2021

    @darshankawar
    Member

    @Kirill-21
    Please provide a complete minimal code sample.
    Also can you elaborate on if config file is broken. with an example ?

  2. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 31, 2021
  3. krll-kov commented on Aug 31, 2021

    @krll-kov
    ContributorAuthor

    @Kirill-21
    Please provide a complete minimal code sample.
    Also can you elaborate on if config file is broken. with an example ?

    Rename .txt -> .json (github does not allow to upload .json) (if you open it with notepad++, you'll see a lot of NUL NUL NUL NUL...)
    shared_preferences.txt

    add this to dependencies

    dependencies:
      shared_preferences: ^2.0.7
    

    place renamed shared preferences file to your's application folder (%appdata%/com.yourname/yourname [Example: C:\Users\user\AppData\Roaming\com.gskinner\flutter_folio\shared_preferences.json]) and add this into the main() function

    SharedPreferences _preferences = await SharedPreferences.getInstance();
    
  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 31, 2021
  5. darshankawar commented on Aug 31, 2021

    @darshankawar
    Member

    @Kirill-21
    Sorry where do I need to put the file again ?
    I renamed it to shared_preferences.json.

  6. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 31, 2021
  7. krll-kov commented on Aug 31, 2021

    @krll-kov
    ContributorAuthor

    @Kirill-21
    Sorry where do I need to put the file again ?
    I renamed it to shared_preferences.json.

    %appdata%/com.yourname/yourname
    com.yourname you specify when you create a flutter project
    yourname you specify when you create a flutter project

  8. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 31, 2021
  9. darshankawar commented on Aug 31, 2021

    @darshankawar
    Member

    That seems to be a Windows path. I am using Mac and tried putting the file in project's root path and called SharedPreferences _preferences = await SharedPreferences.getInstance(); in main() function, but didn't see the issue. Maybe something is still missing.
    Please be descriptive while providing details.

  10. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 31, 2021
  11. krll-kov commented on Aug 31, 2021

    @krll-kov
    ContributorAuthor

    That seems to be a Windows path. I am using Mac and tried putting the file in project's root path and called SharedPreferences _preferences = await SharedPreferences.getInstance(); in main() function, but didn't see the issue. Maybe something is still missing.
    Please be descriptive while providing details.

    I have no idea where settings files for application is stored on macOs, but i specified, that i face issue on Windows

    When you call

    SharedPreferences _preferences = await SharedPreferences.getInstance();
    

    method(and save at least one parameter), shared preferences creates a shared_preferences file on your device and stores all setting there. Find this file and replace it with the provided one. On windows this file is stored in %appdata%/com.yourdeveloper/yourappname
    image

  12. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 31, 2021
  13. stuartmorgan-g commented on Aug 31, 2021

    @stuartmorgan-g
    Contributor

    I am using Mac and tried [...]

    Windows and macOS have completely unrelated implementations of preference storage. You cannot reproduce a Windows storage issue on macOS regardless of where you try to put that file.

  14. 53 remaining items

  15. flutter-triage-bot commented on Oct 11, 2024

    @flutter-triage-bot

    This issue is assigned to @tarrinneal but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks!

  16. jaxharber commented on Oct 25, 2024

    @jaxharber

    how about in multi engine case?

  17. removed their assignment
    on Oct 25, 2024
  18. removed
    ⏳Bot is counting down the days until it unassigns the issue
    on Oct 25, 2024
  19. albe-jj commented on Jan 28, 2025

    @albe-jj

    One of our users just reported the same problem on windows. The file got corrupted for unknown reasons and the entire app would not be able to start because of it.

    When calling SharedPreferences.getInstance() this error is thrown FormatException: Unexpected character (at character 1)

    The corrupt (left) vs healthy shared_preferences.json file is shown below. The corrupted file looks like a binary.

    Image

    shared_preferences version used: 2.1.2

  20. hiteshpatel1992 commented on Feb 22, 2025

    @hiteshpatel1992
  21. Hairo commented on Mar 31, 2025

    @Hairo

    What about rewritting an empty but valid json file when using .clear() with no parameters? at least on the affected platforms (windows/linux), wouldn't really solve the problem but at least can do something to prevent your progam from crashing on startup.

  22. tomekit commented on Apr 5, 2025

    @tomekit

    I agree, that something as simple as clear() or even getFilePath() (so we can clear ourselves) would really help a lot.

    We've had couple reports on Windows so far.

  23. zhouzhuo810 commented on Jul 28, 2025

    @zhouzhuo810
  24. eliast96 commented on Oct 20, 2025

    @eliast96
  25. trapeye commented on Nov 21, 2025

    @trapeye

    any solution for this? I can't clear the prefrence is corrupt, need manually clear, is there program to delete shared prefrence when it corrupt?

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

    P2Important issues not at the top of the work lista: desktopRunning on desktopp: shared_preferencesPlugin to read and write Shared Preferencespackageflutter/packages repository. See also p: labels.platform-linuxBuilding on or for Linux specificallyplatform-windowsBuilding on or for Windows specificallyteam-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