How to create Model class in flutter?

374
    class Autogenerated {
    String statusMessage;
    String statusCode;
    Null error;
    FinResult finResult;

    Autogenerated({this.statusMessage, this.statusCode, this.error, 
    this.finResult});

        Autogenerated.fromJson(Map<String, dynamic> json) {
        statusMessage = json['statusMessage'];
        statusCode = json['statusCode'];
        error = json['error'];
        finResult = json['finResult'] != null ? new 
        FinResult.fromJson(json['finResult']) : null;
        }

        Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['statusMessage'] = this.statusMessage;
        data['statusCode'] = this.statusCode;
        data['error'] = this.error;
        if (this.finResult != null) {
        data['finResult'] = this.finResult.toJson();
        }
        return data;
        }
        }

        class FinResult {
        int count;
        Result result;
        int start;

        FinResult({this.count, this.result, this.start});

        FinResult.fromJson(Map<String, dynamic> json) {
        count = json['count'];
        result = json['result'] != null ? new Result.fromJson(json['result']) : 
        null;
        start = json['start'];
        }

        Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['count'] = this.count;
        if (this.result != null) {
        data['result'] = this.result.toJson();
        }
        data['start'] = this.start;
        return data;
        }
        }

        class Result {
        List<GeneratedAllocation> generatedAllocation;
        Null executedAllocation;

        Result({this.generatedAllocation, this.executedAllocation});

        Result.fromJson(Map<String, dynamic> json) {
        if (json['generatedAllocation'] != null) {
            generatedAllocation = new List<GeneratedAllocation>();
            json['generatedAllocation'].forEach((v) { generatedAllocation.add(new 
        GeneratedAllocation.fromJson(v)); });
        }
        executedAllocation = json['executedAllocation'];
    }

        Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        if (this.generatedAllocation != null) {
        data['generatedAllocation'] = this.generatedAllocation.map((v) => 
        v.toJson()).toList();
    }
        data['executedAllocation'] = this.executedAllocation;
        return data;
    }
}

       class GeneratedAllocation {
       List<List> subAssetClassWeight;
       double weight;
       String asset;

       GeneratedAllocation({this.subAssetClassWeight, this.weight, this.asset});

        GeneratedAllocation.fromJson(Map<String, dynamic> json) {
        if (json['sub_asset_class_weight'] != null) {
            subAssetClassWeight = new List<List>();
            json['sub_asset_class_weight'].forEach((v) { subAssetClassWeight.add(new 
        List.fromJson(v)); });
        }
        weight = json['weight'];
        asset = json['asset'];
    }

        Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        if (this.subAssetClassWeight != null) {
        data['sub_asset_class_weight'] = this.subAssetClassWeight.map((v) => v.toJson()).toList();
    }
        data['weight'] = this.weight;
        data['asset'] = this.asset;
        return data;
    }
}

        class SubAssetClassWeight {


       SubAssetClassWeight({});

       SubAssetClassWeight.fromJson(Map<String, dynamic> json) {
    }

        Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        return data;
    }
}
Share:
374
Pallavi Tapkir
Author by

Pallavi Tapkir

Android developer

Updated on January 01, 2023

Comments

  • Pallavi Tapkir
    Pallavi Tapkir over 1 year

    I have response like below

    {"statusMessage":"Get details successfully","statusCode":"200","error":null,"finResult":{"count":0,"result":{"generatedAllocation":[{"sub_asset_class_weight":[["Large-Cap",100.0]],"weight":40.01051173604412,"asset":"Equity"},{"sub_asset_class_weight":[["Low Duration",7.052961633686329],["Ultra Short Duration",51.01188034311372],["Short Duration",41.93515802319997]],"weight":59.98948826395587,"asset":"FixedIncome"}],"executedAllocation":null},"start":0}}
    

    I want to create a model class for above response. I also tried JsonToDart But it shows error "The Dart code generated is invalid". I want access sub_asset_class_weight property from all objects.

    Any help will be appreciated. TIA

    • Ravindra S. Patil
      Ravindra S. Patil over 2 years
      add your code snippet
  • Umair Ahmad
    Umair Ahmad over 2 years
    You can use online tool for this as i have used. Or there's other option, you can also use, JSON Searilizer package from pub.dev.