This is Food
class Food {
String imgUrl;
String desc;
String name;
String waitTime;
num score;
int price;
int quantity;
List<Map<String, String>> ingredients;
String about;
bool highlight;
Food(this.imgUrl, this.desc, this.name, this.waitTime, this.score, this.price,
this.quantity, this.ingredients, this.about,
{this.highlight = false});
}
I have a list of Food
named userOrders
and I'm trying to create a provider function that will increment and decrement the quantity
paramter when I call it but of the specific instance of Food
I'm dealing with.
For example:
class FoodCounter with ChangeNotifier {
int _count = userOrders[0].quantity;
int get count => _count;
void increment() {
_count++;
notifyListeners();
}
void decrement() {
_count--;
notifyListeners();
}
void reset() {
_count = 0;
notifyListeners();
}
void update() {
notifyListeners();
}
}
I'm trying to do something like this but the problem is I can only modify the first element in the list of food. I'm wondering if I can do something like int _count = userOrders[element].quantity;
or int _count = userOrders[context].quantity;
Try using the following code to manage quantity and total count.
class FoodCounter with ChangeNotifier {
FoodCounter(){
for(final order in userOrders) {
_count += order.quantity;
}
}
int _count = 0;
int get count => _count;
void increment(UserOrder order) {
order.quantity += 1;
_count++;
notifyListeners();
}
void decrement(UserOrder order) {
if (order.quantity > 0) {
order.quantity -= 1;
_count--;
notifyListeners();
}
}
void reset() {
_count = 0;
for(final order in userOrders) {
order.quantity = 0;
}
notifyListeners();
}
void update() {
notifyListeners();
}
}