Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>' in Flutter

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&quot;
}

   // 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;
      }
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading