how to add data to json file flutter?

54
May 11, 2022, at 4:40 PM

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%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.qEvRk2ip9vubQ-_xnfTj1AHaFj%26pid%3DApi&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%3A%2F%2Ftse3.mm.bing.net%2Fth%3Fid%3DOIP.l5ZNKHxygp8AHCJbVwlUaAHaE8%26pid%3DApi&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%3A%2F%2Ftse2.mm.bing.net%2Fth%3Fid%3DOIP.hRWr-pLb_cF-gOn31CCSJgHaHa%26pid%3DApi&f=1",
    "p_name":"Bananna",
    "p_id":3,
    "p_cost":5,
    "p_availability":0
  },
  {
    "p_image": "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse2.mm.bing.net%2Fth%3Fid%3DOIP.FFvWgkIBYYekEc8BVt_wHgHaFW%26pid%3DApi&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;
}
}
Answer 1

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.

Answer 2

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

[
 {
   "p_image": "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.EvRk2ip9vubQ-_xnfTj1AHaFj%26pid%3DApi&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%3A%2F%2Ftse3.mm.bing.net%2Fth%3Fid%3DOIP.l5ZNKHxygp8AHCJbVwlUaAHaE8%26pid%3DApi&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%3A%2F%2Ftse2.mm.bing.net%2Fth%3Fid%3DOIP.hRWr-pLb_cF-gOn31CCSJgHaHa%26pid%3DApi&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%3A%2F%2Ftse2.mm.bing.net%2Fth%3Fid%3DOIP.FFvWgkIBYYekEc8BVt_wHgHaFW%26pid%3DApi&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;
 }
}
READ ALSO
Android MenuItem not always initialized

Android MenuItem not always initialized

I have an activity with three MenuItem'sI have three variables at the top of the activity class for each MenuItem (e

70
App is crashing when i click on any item in recyclerview

App is crashing when i click on any item in recyclerview

I was creating a recycler view and trying to create a toast when any item is clicked on using interface(i'm doing for first time)But my app is crashing when i click any item in recycler view

56
Kotlin: Subscribe to Firebase Cloud Messaging(FCM) Topic

Kotlin: Subscribe to Firebase Cloud Messaging(FCM) Topic

I've used the Android (Java) way of doing things before for subscribing to a topic

67
Lib Gdx what is the error and how to solve it?

Lib Gdx what is the error and how to solve it?

When I create a bundle, I get this errorWhen I create an apk or run directly from Android Studio, there are no errors

61