首页 > 解决方案 > 如何在 Flutter 中更新复杂模型中的值?

问题描述

在我的颤振应用程序中,我有 5 个用于构建的参数 Likeeleveator,storeroom,parking,buildAge,rentPrice这些参数的默认值一开始是 0 ,我想ApartemanRentOptionModel在不同的步骤中更新这个类中的每个值而不更改其他值,最后将完整的值发送到服务器。

我有一个像这样的出租Apartemans选项课程:

    class ApartemanRentOptionModel {
    ApartemanRentOptionModel({
        this.eleveator,
        this.storeroom,
        this.parking,
        this.buildAge,
        this.rentPrice
       
    });

   
    bool eleveator;
    bool storeroom;
    bool parking;
    List<BuildAge> buildAge;
    List<RentPrice> rentPrice;
   

    factory ApartemanRentOptionModel.fromJson(Map<String, dynamic> json) => ApartemanRentOptionModel(
        eleveator: json["eleveator"],
        storeroom: json["storeroom"],
        parking: json["parking"],
        buildAge: List<BuildAge>.from(json["buildAge"].map((x) => BuildAge.fromJson(x))),
        rentPrice: List<RentPrice>.from(json["rentPrice"].map((x) => RentPrice.fromJson(x))),
        
    );

    Map<String, dynamic> toJson() => {
        "eleveator": eleveator,
        "storeroom": storeroom,
        "parking": parking,
        "buildAge": List<dynamic>.from(buildAge.map((x) => x.toJson())),
        "rentPrice": List<dynamic>.from(rentPrice.map((x) => x.toJson())),
        };
    }
 
  class BuildAge {
    BuildAge({
        this.buildAgeId,
        this.buildAgeTitle,
        this.buildAgeValue,
    });

    String buildAgeId;
    String buildAgeTitle;
    int buildAgeValue;

    factory BuildAge.fromJson(Map<String, dynamic> json) => BuildAge(
        buildAgeId: json["buildAgeID"],
        buildAgeTitle: json["buildAgeTitle"],
        buildAgeValue: json["buildAgeValue"],
    );

    Map<String, dynamic> toJson() => {
        "buildAgeID": buildAgeId,
        "buildAgeTitle": buildAgeTitle,
        "buildAgeValue": buildAgeValue,
    };
}

class RentPrice {
    RentPrice({
        this.rentPriceId,
        this.rentPriceTitle,
        this.rentPriceValue,
    });

    String rentPriceId;
    String rentPriceTitle;
    double rentPriceValue;

    factory RentPrice.fromJson(Map<String, dynamic> json) => RentPrice(
        rentPriceId: json["rentPriceID"],
        rentPriceTitle: json["rentPriceTitle"],
        rentPriceValue: json["rentPriceValue"].toDouble(),
    );

    Map<String, dynamic> toJson() => {
        "rentPriceID": rentPriceId,
        "rentPriceTitle": rentPriceTitle,
        "rentPriceValue": rentPriceValue,
    };
}

我需要更改某些数据中的值,例如BuildAgeRentPrice使用这样的函数:

    ApartemanRentOptionModel _currentApartemanData;

    changeCurretAparemanData(newdata) {
      _currentApartemanData.toJson().update("BuildAge", (value) => newdata)
      notifyListeners();
    return null;
  }

但它不起作用,没有任何变化,请帮助我如何在几次内更新单个模型类的不同值。谢谢

标签: flutter

解决方案


您可以在模型中使用 getter 或 setter 函数 .getter 函数用于从模型中获取值,而 setter 用于在模型中设置或更新值。


推荐阅读