英文:
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.
{
    "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.
答案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<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();
  }
}
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<double> _price_range;
To
List<double>? _price_range;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论