Skip to content

Instantly share code, notes, and snippets.

@ffkev
Created September 5, 2024 11:42
Show Gist options
  • Save ffkev/2f5444f9f350ea62e7fd1d8903656167 to your computer and use it in GitHub Desktop.
Save ffkev/2f5444f9f350ea62e7fd1d8903656167 to your computer and use it in GitHub Desktop.
Code that will modify the isSelected value in JSON based on what values are passed to the parser function
import 'dart:convert';
void main() {
String jsonString = '''
{
"baseRequest": [
{"iconCode": 58513, "chipLabel": "India", "isDisabled": true, "isSelected": false},
{"iconCode": 58513, "chipLabel": "Africa", "isDisabled": true, "isSelected": false},
{"iconCode": 58395, "chipLabel": "America", "isDisabled": false, "isSelected": true},
{"iconCode": 58205, "chipLabel": "England", "isDisabled": false, "isSelected": false},
{"iconCode": 58224, "chipLabel": "Georgeia", "isDisabled": true, "isSelected": true}
]
}
''';
Map<String, dynamic> parsedJson = json.decode(jsonString);
void updateIsSelected(int indexToUpdate, bool valueToUpdate) {
if (indexToUpdate >= 0 && indexToUpdate < parsedJson['baseRequest'].length) {
parsedJson['baseRequest'][indexToUpdate]['isSelected'] = valueToUpdate;
} else {
print('Index out of range');
}
}
// Example usage
updateIsSelected(2, false);
print(parsedJson);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment