sony / flutter-elinux-plugins

Flutter plugins for embedded Linux (eLinux)
BSD 3-Clause "New" or "Revised" License
43 stars 23 forks source link

【SharedPreferencesELinux】Unable to save #90

Open JGNS opened 7 months ago

JGNS commented 7 months ago

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 7 months ago

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 7 months ago

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 7 months ago

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.