How to Iterate Through All Keys of Shared Preferences

How to iterate through all keys of shared preferences?

What you can do is use getAll() method of SharedPreferences and get all the values in Map<String,?> and then you can easily iterate through.

Map<String,?> keys = prefs.getAll();

for(Map.Entry<String,?> entry : keys.entrySet()){
Log.d("map values",entry.getKey() + ": " +
entry.getValue().toString());
}

For more you can check PrefUtil.java's dump() implementation.

How to iterate through all keys of shared preferences?

What you can do is use getAll() method of SharedPreferences and get all the values in Map<String,?> and then you can easily iterate through.

Map<String,?> keys = prefs.getAll();

for(Map.Entry<String,?> entry : keys.entrySet()){
Log.d("map values",entry.getKey() + ": " +
entry.getValue().toString());
}

For more you can check PrefUtil.java's dump() implementation.

How can I Loop through data that is stored in my sharedpreference with a key in - Flutter

Here is an example for you to achieve your scenario..

   var user;
var userData;
List anchors = [];

_getUserAnchor() async{
SharedPreferences localStorage = await SharedPreferences.getInstance();
user = userJson;
setState(() {
anchors = user['Anchors'];
});
print(anchors);
setState(() {
userData = anchors;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('test'),),
body: Column(
children: <Widget>[
ListView.builder(
shrinkWrap: true,
itemCount: anchors.length,
itemBuilder: (BuildContext context, int i){
return Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: Column(
children: <Widget>[
Text(anchors[i]['Name']),
Text(anchors[i]['Oid'].toString()),
],
),
),
);
})
],
),
);
}

Here I didn't get data from shared preferences i have equal your json to userJson, U can use your same steps get the data from shared preferences and follow up, So you can build list of card's.. hope this will help you...

Looping through SharedPreferences using Map

Thanks to crgarridos I managed to see my mistake. It was actually quite a silly one. I had just forgotten to store a value into SharedPreferences. With this code in MySecondActivity, it is working:

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("First key", var1);
editor.putInt("Second key", var2);
editor.putInt("Third key", var3); //<- Hehe...

// Commit the edits!
editor.commit();

Get all SharedPreferences names and all their keys?

Get all the _preferences.xml from

/data/data/(package)/shared_prefs/(package)_preferences.xml

Parse the XML, in the XML there is name for every key. I might misunderstood your question



Related Topics



Leave a reply



Submit