Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【SharedPreferencesELinux】Unable to save #90

Open
JGNS opened this issue Dec 5, 2023 · 3 comments
Open

【SharedPreferencesELinux】Unable to save #90

JGNS opened this issue Dec 5, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@JGNS
Copy link

JGNS commented Dec 5, 2023

i run main.dart, always "Button tapped 1 time", when i restart App, it become "0 times".I just added some printing:

Future _incrementCounter() async {
final Map<String, Object> values = await prefs.getAll();
final int counter = (values['counter'] as int? ?? 0) + 1;
print('_incrementCounter:values:$values');
setState(() {
_counter = prefs.setValue('Int', 'counter', counter).then((bool success) {
print('_incrementCounter:success:$success');
return counter;
});
});
}

No matter how many times i click,output always:
_incrementCounter:values:{}
_incrementCounter:success:true
...

No error message is reported。
so,where might the problem occur?
Looking forward to your reply,
thanks.

@JGNS
Copy link
Author

JGNS commented Dec 5, 2023

I found the json file under ~/.local/share/app_name. The content inside is always {"count": 1}, which is not affected by clicks.

@JGNS
Copy link
Author

JGNS commented Dec 5, 2023

My guess is that it cannot be read, and the result of each read is null. So the result of counter = (values['counter'] as int? ?? 0) + 1 is 1, write 1 again.

@JGNS
Copy link
Author

JGNS commented Dec 6, 2023

I located the problem, which is _defaultPrefix. The definitions of key between getAllWithParameters and setValue are not consistent. I can temporarily use final Map<String, Object> values = await prefs.getAllWithPrefix(""); to solve the problem.

@HidenoriMatsubayashi HidenoriMatsubayashi added the bug Something isn't working label Dec 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants