I would like to save the sharedpreferences keys on my smartphone. The keys are table id's (autoincrements) from a table. Now i would like to show these favorite records in a new tab.
How can i select with these keys in my database table? Or is it possible to compare them with all records in a json dataset what i select at the beginning to show all records from the table?
At the moment i'm saving the keys in Strings. And then showing them in a listview.
Future<void> _save() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final key = '${widget.id}';
final value = '${widget.id}';
prefs.setString(key, value);
}
Future<List<Widget>> getAllPrefs() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//final SharedPreferences prefs = await PrefStore().prefs;
return prefs
.getKeys()
.map<Widget>((key) => ListTile(
title: Text(key),
subtitle: Text(prefs.get(key).toString()),
))
.toList(growable: false);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<List<Widget>>(
future: getAllPrefs(),
builder: (context, snapshot) {
if (!snapshot.hasData) return Container();
return ListView(
children: snapshot.data,
);
}),
);
}
Thank you very much.
I have got a good idea which works fine, see below. But if you have a better solution please post it too.
I used only one future for getInstance and SQL-Select.
// get the keys, convert to string, change {1,2,3} to (1,2,3) with strings/substring
SharedPreferences prefs = await SharedPreferences.getInstance();
String allkeys = prefs.getKeys().toString();
keylist = '(' + allkeys.substring(1, allkeys.length - 1) + ')';
Then select with keylist as followed:
select * from table where table.id in $keylist
// means: ... where table.id in (1,2,3)
If you need more code, please comment, thx