Home > other >  how to add data to json file flutter?
how to add data to json file flutter?

Time:02-08

i have a data.json file like this in which i am getting a list of data i want to add another parameter{p_quantity} to this json file . Or i can the parameter by default as empty then how i can i update that value .

[
  {
    "p_image": "https://external-content.duckduckgo.com/iu/?u=https://tse1.mm.bing.net/th?id=OIP.qEvRk2ip9vubQ-_xnfTj1AHaFj&pid=Api&f=1",
    "p_name":"Apple",
    "p_id":1,
    "p_cost":30,
    "p_availability":1,
    "p_details":"Imported from Swiss",
    "p_category":"Premium"
  },
  {
    "p_image": "https://external-content.duckduckgo.com/iu/?u=https://tse3.mm.bing.net/th?id=OIP.l5ZNKHxygp8AHCJbVwlUaAHaE8&pid=Api&f=1",
    "p_name":"Mango",
    "p_id":2,
    "p_cost":50,
    "p_availability":1,
    "p_details":"Farmed at Selam",
    "p_category":"Tamilnadu"
  },
  {
    "p_image": "https://external-content.duckduckgo.com/iu/?u=https://tse2.mm.bing.net/th?id=OIP.hRWr-pLb_cF-gOn31CCSJgHaHa&pid=Api&f=1",
    "p_name":"Bananna",
    "p_id":3,
    "p_cost":5,
    "p_availability":0
  },
  {
    "p_image": "https://external-content.duckduckgo.com/iu/?u=https://tse2.mm.bing.net/th?id=OIP.FFvWgkIBYYekEc8BVt_wHgHaFW&pid=Api&f=1",
    "p_name":"Orange",
    "p_id":4,
    "p_cost":25,
    "p_availability":1,
    "p_details":"from Nagpur",
    "p_category":"Premium"
  }
]

in which i want to add another parameter "p_quantity" . how can i do that ? I am getting the data via this function

Future<List<Products>> readJsonData() async {
  //read json file
  final jsondata = await rootBundle.rootBundle.loadString('assets/data.json');
  //decode json data as list
  final list = json.decode(jsondata) as List<dynamic>;
  //map json and initialize using DataModel
  return list.map((e) => Products.fromJson(e)).toList();
}

and here is the product class

class  Products {
String? pImage;
String? pName;
int? pId;
int? pCost;
int? pAvailability;
String? pDetails;
String? pCategory;

Products(
{this.pImage,
this.pName,
this.pId,
this.pCost,
this.pAvailability,
this.pDetails,
this.pCategory});

Products.fromJson(Map<String, dynamic> json) {
pImage = json['p_image'];
pName = json['p_name'];
pId = json['p_id'];
pCost = json['p_cost'];
pAvailability = json['p_availability'];
pDetails = json['p_details'];
pCategory = json['p_category'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['p_image'] = pImage;
data['p_name'] = pName;
data['p_id'] = pId;
data['p_cost'] = pCost;
data['p_availability'] = pAvailability;
data['p_details'] = pDetails;
data['p_category'] = pCategory;
return data;
}
}

CodePudding user response:

You need to create a Map. After that, you can add variables to that Map using this command:

exampleMap["p_data"] = "somedata";

This should work.

CodePudding user response:

You can add parameter p_quantity by manually in you json file like this

[
 {
   "p_image": "https://external-content.duckduckgo.com/iu/?u=https://tse1.mm.bing.net/th?id=OIP.EvRk2ip9vubQ-_xnfTj1AHaFj&pid=Api&f=1",
   "p_name": "Apple",
   "p_id": 1,
   "p_cost": 30,
   "p_availability": 1,
   "p_details": "Imported from Swiss",
   "p_category": "Premium",
   "p_quantity": 0
  },
  {
  "p_image": "https://external-content.duckduckgo.com/iu/?u=https://tse3.mm.bing.net/th?id=OIP.l5ZNKHxygp8AHCJbVwlUaAHaE8&pid=Api&f=1",
  "p_name": "Mango",
  "p_id": 2,
  "p_cost": 50,
  "p_availability": 1,
  "p_details": "Farmed at Selam",
  "p_category": "Tamilnadu",
  "p_quantity": 4
 },
 {
  "p_image": "https://external-content.duckduckgo.com/iu/?u=https://tse2.mm.bing.net/th?id=OIP.hRWr-pLb_cF-gOn31CCSJgHaHa&pid=Api&f=1",
  "p_name": "Bananna",
  "p_id": 3,
  "p_cost": 5,
  "p_availability": 0,
  "p_quantity": 1
 },
 {
  "p_image": "https://external-content.duckduckgo.com/iu/?u=https://tse2.mm.bing.net/th?id=OIP.FFvWgkIBYYekEc8BVt_wHgHaFW&pid=Api&f=1",
  "p_name": "Orange",
  "p_id": 4,
  "p_cost": 25,
  "p_availability": 1,
  "p_details": "from Nagpur",
  "p_category": "Premium",
  "p_quantity": 2
 }
]

And your product class looks like

class  Products {
  String? pImage;
  String? pName;
  int? pId;
  int? pCost;
  int? pAvailability;
  int? pQuantity;
  String? pDetails;
  String? pCategory;

Products(
 {this.pImage,
  this.pName,
  this.pId,
  this.pCost,
  this.pAvailability,
  this.pDetails,
  this.pCategory,
  this.pQuantity});

Products.fromJson(Map<String, dynamic> json) {
  pImage = json['p_image'];
  pName = json['p_name'];
  pId = json['p_id'];
  pCost = json['p_cost'];
  pAvailability = json['p_availability'];
  pDetails = json['p_details'];
  pCategory = json['p_category'];
  pQuantity = json['p_quantity'];
}

Map<String, dynamic> toJson() {
  final Map<String, dynamic> data = <String, dynamic>{};
  data['p_image'] = pImage;
  data['p_name'] = pName;
  data['p_id'] = pId;
  data['p_cost'] = pCost;
  data['p_availability'] = pAvailability;
  data['p_details'] = pDetails;
  data['p_category'] = pCategory;
  data['p_quantity'] = pQuantity;
 return data;
 }
}
  •  Tags:  
  • Related