簡體   English   中英

如何在 Flutter 中更新復雜 Model 中的值?

[英]How can Update values in complex Model in Flutter?

在我的 flutter 應用程序中,我有 5 個建築物參數,如eleveator,storeroom,parking,buildAge,rentPrice這些參數的默認值在開始時為 0,我想在不同的步驟中更新此ApartemanRentOptionModel class 中的每個值而不更改其他值,最后將完整的值發送到服務器。

我有一個 class 用於出租 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,
    };
}

我需要像這樣使用 function 更改BuildAgeRentPrice等數據中的值:

    ApartemanRentOptionModel _currentApartemanData;

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

但它不起作用並且沒有任何變化,請幫助我如何多次更新單個 Model class 的不同值。 謝謝

You can use getter or setter function in model.getter function use for get value from model and setter use for set or update value in model.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM