Flutter Getx RxList 产品列表,名称-价格

Flutter Getx RxList Product List, Name-Price

我想用 Getx 创建一个购物篮,我将值定义为名称和价格,我想将基本产品添加到购物篮。我如何将字符串作为名称或作为价格从 Product 添加到 PriceModel2 -RxList 中?

产品型号-

class PriceModel2 {
  String name;
  double price;

  PriceModel2(String name, double price) {
    this.name = name;
    this.price = price;
  }
}

添加函数

RxList<PriceModel2> basket = [].obs;

  urunEkle(String isim) {
    basket.add(isim);
    basket.add(isim<basket>[name]);
  }

UI

 ElevatedButton(
              onPressed: () {
                controller.urunEkle(
                    material["name"] );

感谢大家的帮助。

试试这个:

型号:

class PriceModel2 {
  final String name;
  final double price;

  PriceModel2( {required this.name,required this.price,});
}

函数:

List<PriceModel2> basket = <PriceModel2>[].obs;

void urunEkle(String isim, double fiyat) {
  basket.add(PriceModel2(name: isim, price: fiyat));
}

Ui:

 ElevatedButton(
    onPressed: () {
      controller.urunEkle("isim", 9.00);
    },

如果您使用空安全,此代码有效