如何创建一个类,其中一个属性是另一个类中属性的生成列表?

huangapple go评论78阅读模式
英文:

How to make a class where one attribute is a generated list of the attributes of another list in the class?

问题

I have a class of prices in a store. It does not accept price_range as an input but I want it to produce a list of all the prices of the products for itself.

{
    "mall": 
    [
        {
            "store_id": 1,
            "price_range": [],
            "products":
            [
                {
                    "product_id": 1,
                    "price": 9.99
                }
            ]
        },
        {
            "store_id": 2,
            "price_range": [],
            "products":
            [
                {
                    "product_id": 1,
                    "price": 45.99
                },
                {
                    "product_id": 2,
                    "price": 25.99
                }
            ]
        }
    ]
}

Is it possible for price_range to have the prices of the products?
For in this case:

store1 can have price_range = [9.99] and
store2 can have price_range = [45.99, 25.99]?

I have appended my model at the bottom. I tried to use this._price_range = product_model.map((element) => double.parse(element.price));. Not sure yet if it is able to create the list of prices. I have removed the ProductModel class to make it shorter.

class Mall {
  late List<mallModel> _mall;
  List<mallModel> get mall => _mall;

  Mall({required mall}){
    this._mall = mall;
  }

  Mall.fromJson(Map<String, dynamic> json) {
    if (json['contents'] != null) {
    _mall = <MallModel>[];
      json['mall'].forEach((v) {
        _mall!.add(MallModel.fromJson(v));
      });
    }
  }
}

class StoreModel {
  int? store_id;
  late List<ProductModel> _product_model;
  List<ProductModel> get product_model => _product_model;

  late List<double> _price_range;
  List<double> get price_range => _price_range;

  StoreModel(
      {required store_id;
        required product_model}){
    this._product_model = product_model;
    this._price_range = product_model.map((element) => double.parse(element.price));
  }

  StoreModel.fromJson(Map<String, dynamic> json) {
    store_id= json['store_id'];
    if (json['products'] != null) {
      _product_model = <ProductModel>[];
      json['products'].forEach((v) {
        _product_model.add(Product.fromJson(v));
      });
     _price_range = [];
    }
  }
}

When I try to run my app, I am getting this error
LateInitializationError: Field '_price_range@22053741' has not been initialized.

英文:

I have a class of prices in a store. It does not accept price_range as an input but I want it to produce a list of all the prices of the products for itself.

{
    &quot;mall&quot;: 
    [
        {
            &quot;store_id&quot;: 1,
            &quot;price_range&quot;: [],
            &quot;products&quot;:
            [
                {
                    &quot;product_id&quot;: 1,
                    &quot;price&quot;: 9.99
                }
            ]
        },
        {
            &quot;store_id&quot;: 2,
            &quot;price_range&quot;: [],
            &quot;products&quot;:
            [
                {
                    &quot;product_id&quot;: 1,
                    &quot;price&quot;: 45.99
                },
                {
                    &quot;product_id&quot;: 2,
                    &quot;price&quot;: 25.99
                }
            ]
        }
    ]
}

Is it possible for price_range to have the prices of the products?
For in this case:

store1 can have price_range = [9.99] and
store2 can have price_range = [45.99, 25.99]?

I have appended my model at the bottom. I tried to use this._price_range = product_model.map((element) =&gt; double.parse(element.price));. Not sure yet if it is able to create the list of prices. I have removed the ProductModel class to make it shorter.

class Mall {
  late List&lt;mallModel&gt; _mall;
  List&lt;mallModel&gt; get mall =&gt; _mall;

  Mall({required mall}){
    this._mall = mall;
  }

  Mall.fromJson(Map&lt;String, dynamic&gt; json) {
    if (json[&#39;contents&#39;] != null) {
    _mall = &lt;MallModel&gt;[];
      json[&#39;mall&#39;].forEach((v) {
        _mall!.add(MallModel.fromJson(v));
      });
    }
  }
}

class StoreModel {
  int? store_id;
  late List&lt;ProductModel&gt; _product_model;
  List&lt;ProductModel&gt; get product_model =&gt;_product_model;

  late List&lt;double&gt; _price_range;
  List&lt;double&gt; get price_range =&gt; _price_range;

  StoreModel(
      {required store_id;
        required product_model}){
    this._product_model = product_model;
    this._price_range = product_model.map((element) =&gt; double.parse(element.price));
  }

  StoreModel.fromJson(Map&lt;String, dynamic&gt; json) {
    store_id= json[&#39;store_id&#39;];
    if (json[&#39;products&#39;] != null) {
      _product_model = &lt;ProductModel&gt;[];
      json[&#39;products&#39;].forEach((v) {
        _product_model.add(Product.fromJson(v));
      });
    }
   _price_range = [];
  }
}

When I try to run my app, I am getting this error
LateInitializationError: Field &#39;_price_range@22053741&#39; has not been initialized.

答案1

得分: 1

你应该在StoreModel.fromJson中初始化_price_range

class StoreModel {
  int? store_id;
  late List<ProductModel> _product_model;
  List<ProductModel> get product_model => _product_model;

  late List<double> _price_range;
  List<double> get price_range => _price_range;

  StoreModel({
    required this.store_id,
    required product_model,
  }) {
    this._product_model = product_model;
    this._price_range = product_model.map((element) => double.parse(element.price)).toList();
  }

  StoreModel.fromJson(Map<String, dynamic> json) {
    store_id = json['store_id'];
    _product_model = <ProductModel>[];
    if (json['products'] != null) {
      json['products'].forEach((v) {
        _product_model.add(Product.fromJson(v));
      });
    }
    this._price_range = product_model.map((element) => double.parse(element.price)).toList();
  }
}

希望这有所帮助!

英文:

You should initiate _price_range in StoreModel.fromJson

class StoreModel {
  int? store_id;
  late List&lt;ProductModel&gt; _product_model;
  List&lt;ProductModel&gt; get product_model =&gt;_product_model;

  late List&lt;double&gt; _price_range;
  List&lt;double&gt; get price_range =&gt; _price_range;

  StoreModel(
      {required this.store_id,
        required product_model}){
    this._product_model = product_model;
    this._price_range = product_model.map((element) =&gt; double.parse(element.price)).toList();
  }

  StoreModel.fromJson(Map&lt;String, dynamic&gt; json) {
    store_id= json[&#39;store_id&#39;];
     _product_model = &lt;ProductModel&gt;[];
    if (json[&#39;products&#39;] != null) {
      json[&#39;products&#39;].forEach((v) {
        _product_model.add(Product.fromJson(v));
      });
    }
    this._price_range = product_model.map((element) =&gt; double.parse(element.price)).toList();
  }
}

I hope this helps!.

答案2

得分: 0

你之所以收到LateInitializationError错误是因为你在初始化变量_price_range之前使用了它。

你可以考虑将它声明为nullable

将以下代码更改为:

List<double>? _price_range;
英文:

The reason you are getting LateInitializationError is because you are using variable _price_range before initializing it.

You can consider making it nullable instead.

Change:

late List&lt;double&gt; _price_range;

To

List&lt;double&gt;? _price_range;

huangapple
  • 本文由 发表于 2023年3月9日 15:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定