Flutter Getx RxList Product List, Name-Price

Issue

i wanna create a Basket with Getx , i defined a values as Name and Price, i wanna add Basic a Product in to Shop Basket. How can i add a String as name or double as Price from Product into PriceModel2 -RxList ?

Product Model-

class PriceModel2 {
  String name;
  double price;

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

Add Function

RxList<PriceModel2> basket = [].obs;

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

UI

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

Thanks for all Help.

Solution

Try this:

Model:

class PriceModel2 {
  final String name;
  final double price;

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

Function:

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);
    },

This code works if you use null safety

Answered By – Mesota22

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *