I am new to flutter and dart I read a lot of web article and documentation but didn't able to figure out how to update JSON field I have a local JSON file like
{
"category": "Happiness",
"quotes":[
{
"quote":"I hope you will find a reason to smile",
"favorite":false
},
{
"quote":"Sometimes your joy is the source of your smile, but sometimes your smile can be the source of your joy.",
"favorite":false
}]}
I want to update the filed favorite to true in JSON file Is there any way to do this
You can use a forEach loop on the quotes
list in the JSON and set favourite
to true
inside it. I'm assuming you have a parsed your String
data into JSON using jsonDecode
or json.decode
.
If not, here's a link on how to parse JSON data in Dart.
Consider the below code -
Map<String, dynamic> jsonData = {
"category": "Happiness",
"quotes":[
{
"quote":"I hope you will find a reason to smile",
"favorite":false
},
{
"quote":"Sometimes your joy is the source of your smile, but sometimes your smile can be the source of your joy.",
"favorite":false
}
]};
(jsonData["quotes"] as List<dynamic>).forEach((item) {
item["favorite"] = true;
});
You can also alternatively use shorthand syntax for the forEach
loop function like below -
(jsonData["quotes"] as List<dynamic>).forEach((item) => item["favorite"] = true);
first take this json array to any variable and then understand the hierarchy .
e.g "jsonArr" is the array that hold that data then ..
jsonArr['quotes'][0]['favorite'] = true;
or you can put this in a for loop and traverse this loop to the length of the json array.
You Can parse the JSON like this
Map<String, dynamic> data = jsonDecode(jsonString);
And if you want to make each favorite true then You can use looping like this
(data["quotes"] as List<dynamic>).forEach((item) => item["favorite"] = true);
if you want to set a single object value true
then you need to pass the position
like this
(data["quotes"] as List<dynamic>)[position]['favorite'] = true;
Addition
Here is the link to encode and decode the JSON
You can use the following method. The field favorite
is changed to true
when the button is pressed.
FlatButton(
child: Text('Add Favourite'),
onPressed: (){
myJson['quotes'][myIndex]['favorite'] = true;
})
If you use JSON models and declare them as mutable, then you will be able to perform this modification directly on the source data.
import 'dart:convert';
import 'json_objects.dart';
void main() {
var json = jsonDecode(_source) as Map<String, dynamic>;
var response = Response1.fromJson(json);
for (var quote in response.quotes) {
quote.favorite = false;
}
json = response.toJson();
print(json);
}
final _source = r'''
{
"category": "Happiness",
"quotes": [
{
"quote": "I hope you will find a reason to smile",
"favorite": false
},
{
"quote": "Sometimes your joy is the source of your smile, but sometimes your smile can be the source of your joy.",
"favorite": false
}
]
}''';
Reuult:
{category: Happiness, quotes: [{favorite: false, quote: I hope you will find a reason to smile}, {favorite: false, quote: Sometimes your joy is the source of your smile, but sometimes your smile can be the source of your joy.}]}
Used JSON models.
class Response1 {
String category;
List<Response1Quotes> quotes;
Response1({this.category, this.quotes});
factory Response1.fromJson(Map<String, dynamic> json) {
return Response1(
category: json['category'] as String,
quotes: _toObjectList(json['quotes'], (e) => Response1Quotes.fromJson(e)),
);
}
Map<String, dynamic> toJson() {
return {
'category': category,
'quotes': _fromList(quotes, (e) => e.toJson()),
};
}
}
class Response1Quotes {
bool favorite;
String quote;
Response1Quotes({this.favorite, this.quote});
factory Response1Quotes.fromJson(Map<String, dynamic> json) {
return Response1Quotes(
favorite: json['favorite'] as bool,
quote: json['quote'] as String,
);
}
Map<String, dynamic> toJson() {
return {
'favorite': favorite,
'quote': quote,
};
}
}
List _fromList(data, Function(dynamic) toJson) {
if (data == null) {
return null;
}
var result = [];
for (var element in data) {
var value;
if (element != null) {
value = toJson(element);
}
result.add(value);
}
return result;
}
List<T> _toObjectList<T>(data, T Function(Map<String, dynamic>) fromJson) {
if (data == null) {
return null;
}
var result = <T>[];
for (var element in data) {
T value;
if (element != null) {
value = fromJson(element as Map<String, dynamic>);
}
result.add(value);
}
return result;
}
/*
Response1:
"category": String
"quotes": List<Response1Quotes>
Response1Quotes:
"quote": String
"favorite": bool
*/