Home > Mobile >  How to initialize a new variable in an existing model in Flutter
How to initialize a new variable in an existing model in Flutter

Time:10-18

I am calling an api and it returns a json data and this data i embedding with the model by using online converter. Below is the sample json data and model

JSON

{
    "message": "menues returned by Dish Type successfully",
    "data": [
        {
            "id": "10"
        }
    ]
}

Model

class DishMenuTypesIdData {
  String? id;
  DishMenuTypesIdData({
    this.id,
  });
  DishMenuTypesIdData.fromJson(Map<String, dynamic> json) {
    id = json["id"]?.toString();
  }
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data["id"] = id;
    return data;
  }
}
class DishMenuTypesId {
  String? message;
  List<DishMenuTypesIdData?>? data;
  DishMenuTypesId({
    this.message,
    this.data,
  });
  DishMenuTypesId.fromJson(Map<String, dynamic> json) {
    message = json["message"]?.toString();
  if (json["data"] != null) {
  final v = json["data"];
  final arr0 = <DishMenuTypesIdData>[];
  v.forEach((v) {
  arr0.add(DishMenuTypesIdData.fromJson(v));
  });
    this.data = arr0;
    }
  }
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data["message"] = message;
    if (this.data != null) {
      final v = this.data;
      final arr0 = [];
  v!.forEach((v) {
  arr0.add(v!.toJson());
  });
      data["data"] = arr0;
    }
    return data;
  }
}

Now i decided to add a variable qty and initialize qty in existing model which will be

int qty;

And in constructor it will be

this.qty = 1;

But when i call this qty in ui then it is getting null. Although i initialize this qty in constructor equals 1

Model After including qty

class DishMenuTypesIdData {

  String? qty;
  String? id;

  DishMenuTypesIdData({
    this.qty = "1",
    this.id,
  });
  DishMenuTypesIdData.fromJson(Map<String, dynamic> json) {
    qty = json["qty"]?.toString();
    id = json["id"]?.toString();
  }
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data["qty"] = qty;
    data["id"] = id;
    return data;
  }
}

class DishMenuTypesId {

  String? message;
  List<DishMenuTypesIdData?>? data;

  DishMenuTypesId({
    this.message,
    this.data,
  });
  DishMenuTypesId.fromJson(Map<String, dynamic> json) {
    message = json["message"]?.toString();
  if (json["data"] != null) {
  final v = json["data"];
  final arr0 = <DishMenuTypesIdData>[];
  v.forEach((v) {
  arr0.add(DishMenuTypesIdData.fromJson(v));
  });
    this.data = arr0;
    }
  }
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data["message"] = message;
    if (this.data != null) {
      final v = this.data;
      final arr0 = [];
  v!.forEach((v) {
  arr0.add(v!.toJson());
  });
      data["data"] = arr0;
    }
    return data;
  }
}

CodePudding user response:

The server is not returning the qty when being called, thus the qty will be null.

qty = json["qty"]?.toString();

if you want to initialize it to 1, you need to replace this line with:

qty = "1";
  • Related