I am new to flutter and I am in need of some help.
I have a list of objects here.
List<ProductOrdered> myList = [product1, product2, product3];
This is how the three ProductOrdered objects looks like:
ProductOrdered product1 = ProductOrdered(
name: 'Caramida 16x16 chestie, rosu, chestie chestie chestie ',
price: '4.99',
supplierName: 'Dedeman Leroy Merlin',
nr: '12345678',
day: '08',
month: '02',
year: '2002',
total: '31.99',
category: 'Solide',
);
ProductOrdered product2 = ProductOrdered(
name: 'Ciment 16x16 chestie, rosu, chestie chestie chestie ',
price: '3.99',
supplierName: 'Dedeman',
nr: '21345678',
day: '09',
month: '02',
year: '2002',
total: '41.99',
category: 'Prafoase',
);
ProductOrdered product3 = ProductOrdered(
name: 'Tigla 16x16 chestie, rosu, chestie chestie chestie ',
price: '5.99',
supplierName: 'Leroy Merlin',
nr: '31245678',
day: '10',
month: '02',
year: '2002',
total: '51.99',
category: 'Acoperis',
);
And I would like to sort the elements in this list by their prices, when a button is clicked. First time the button is clicked I want to make it ascending, second time descending. However I got stuck at the first part.
I defined a method
_onSortPrice(list) {
setState(() {
list.sort((a, b) => a.price.compareTo(b.price));
});
}
And I called it when the button was clicked
TextButton(
onPressed: () {
_onSortPrice(myList);
},
Nothing happens when I click it. I tried to remove the SetState from the funcion or used just sort method outside the button to see if the page starts with the objects sorted, and it does.
Thanks for reading.