sony/flutter-elinux-plugins

【SharedPreferencesELinux】Unable to save

JGNS opened this issue · 3 comments

JGNS commented

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 commented

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

JGNS commented

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 commented

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.