skip to Main Content

Trying to parse a json file from asset and show the username in Textview. But while trying to assign the data to the model class then the error is coming. All the fields are in String then also the String conversion error is coming.

{
"packageId": "1",
"packageName": "Flexi Package",
"packageStartDate": "18-04-2023",
"packageEndDate": "21-04-2023",
"packageDays": "3N/2D",
"packageOfferedPrice": "23000",
"packageDiscountedPrice": "20000",
"packageHotels": "2",
"packageCars": "3",
"packageThumbnail": "https://i.dummyjson.com/data/products/8/thumbnail.jpg"
}

   // Flutter Code
    loadJson() async {
        String data = await rootBundle.loadString('asset/hotel.json');
         //jsonResult = json.decode(data);
    
        var encodedString = jsonEncode(data);
        Map<String, dynamic> valueMap = json.decode(encodedString);  
        Package user = Package.fromJson(valueMap); // here the error is coming
    
        print(user.packageName);
      }

// Model Class 

    class Package {
      String? packageId;
      String? packageName;
      String? packageStartDate;
      String? packageEndDate;
      String? packageDays;
      String? packageOfferedPrice;
      String? packageDiscountedPrice;
      String? packageHotels;
      String? packageCars;
      String? packageThumbnail;
    
      Package(
          {this.packageId,
            this.packageName,
            this.packageStartDate,
            this.packageEndDate,
            this.packageDays,
            this.packageOfferedPrice,
            this.packageDiscountedPrice,
            this.packageHotels,
            this.packageCars,
            this.packageThumbnail});
    
      Package.fromJson(Map<String, dynamic> json) {
        packageId = json['packageId'];
        packageName = json['packageName'];
        packageStartDate = json['packageStartDate'];
        packageEndDate = json['packageEndDate'];
        packageDays = json['packageDays'];
        packageOfferedPrice = json['packageOfferedPrice'];
        packageDiscountedPrice = json['packageDiscountedPrice'];
        packageHotels = json['packageHotels'];
        packageCars = json['packageCars'];
        packageThumbnail = json['packageThumbnail'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = Map<String, dynamic>();
        data['packageId'] = packageId;
        data['packageName'] = this.packageName;
        data['packageStartDate'] = this.packageStartDate;
        data['packageEndDate'] = this.packageEndDate;
        data['packageDays'] = this.packageDays;
        data['packageOfferedPrice'] = this.packageOfferedPrice;
        data['packageDiscountedPrice'] = this.packageDiscountedPrice;
        data['packageHotels'] = this.packageHotels;
        data['packageCars'] = this.packageCars;
        data['packageThumbnail'] = this.packageThumbnail;
        return data;
      }
    }

2

Answers


  1. var encodedString = jsonEncode(data);
    

    Remove that line. Your file is already a json text. You decode it a line later. There is no point in encoding it in JSON, because then it is encoded twice.

    Login or Signup to reply.
  2. Try this:

    /// Dont forget to import this:
    import 'dart:convert';
    
    ....
    
       // Flutter Code
        loadJson() async {
           String data = await rootBundle.loadString('asset/hotel.json');
           //jsonResult = json.decode(data); //this was the right attemd
        
           // var encodedString = jsonEncode(data);// this turns an Object to Json String. And you are trying to turn a String to a JSON String. Why?
    
           // If your rootBundle works correct and delivers a json data, this will work: 
            Map<String, dynamic> valueMap = jsonDecode(data);
            Package user = Package.fromJson(valueMap); // here the error is coming
        
            print(user.packageName);
          }
    

    Since I havent installed rootbundle, I use this as "await rootBundle.loadString(‘asset/hotel.json’)" (and it worked):

     String data =
            '{ "packageId": "1", "packageName": "Flexi Package", "packageStartDate": "18-04-2023", "packageEndDate": "21-04-2023", "packageDays": "3N/2D", "packageOfferedPrice": "23000", "packageDiscountedPrice": "20000", "packageHotels": "2", "packageCars": "3", "packageThumbnail": "https://i.dummyjson.com/data/products/8/thumbnail.jpg" }';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search