You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[shared_preferences] [windows,linux] Data can be corrupted due to lack of atomic file writes #89211
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
@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
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.
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
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
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.
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!
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.
shared_preferences version used: 2.1.2
Reacted by 牛肉丸, eliast96, LX, willatkova, Imtiaz Ahmad, Yusuf Almaududi and Matthias Schicker
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.
Activity
darshankawar commentedon Aug 31, 2021
@Kirill-21
Please provide a complete minimal code sample.
Also can you elaborate on
if config file is broken.with an example ?krll-kov commentedon Aug 31, 2021
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
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
darshankawar commentedon Aug 31, 2021
@Kirill-21
Sorry where do I need to put the file again ?
I renamed it to
shared_preferences.json.krll-kov commentedon Aug 31, 2021
%appdata%/com.yourname/yourname
com.yourname you specify when you create a flutter project
yourname you specify when you create a flutter project
darshankawar commentedon Aug 31, 2021
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();inmain()function, but didn't see the issue. Maybe something is still missing.Please be descriptive while providing details.
krll-kov commentedon Aug 31, 2021
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
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

stuartmorgan-g commentedon Aug 31, 2021
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.
53 remaining items
flutter-triage-bot commentedon Oct 11, 2024
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!
jaxharber commentedon Oct 25, 2024
how about in multi engine case?
albe-jj commentedon Jan 28, 2025
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 thrownFormatException: Unexpected character (at character 1)The corrupt (left) vs healthy
shared_preferences.jsonfile is shown below. The corrupted file looks like a binary.shared_preferences version used: 2.1.2
hiteshpatel1992 commentedon Feb 22, 2025
Hairo commentedon Mar 31, 2025
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.tomekit commentedon Apr 5, 2025
I agree, that something as simple as
clear()or evengetFilePath()(so we can clear ourselves) would really help a lot.We've had couple reports on Windows so far.
zhouzhuo810 commentedon Jul 28, 2025
eliast96 commentedon Oct 20, 2025
trapeye commentedon Nov 21, 2025
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?