I am trying to calculate set of values based on user selection. I am using the reduce method to calculate it.
Its checking for the respective values in the list and throws this error since there is no data in the list.
I am struck in how to resolve this, below i have attached the code.
get products => _products;
get productSubtotal => _products.entries
.map((product) => product.key.price * product.value)
.toList();
get total => _products.entries
.map((product) => product.key.price * product.value)
.toList()
.reduce((value, element) => value + element)
.toStringAsFixed(2);
}
below i have attached the error message.
Bad state: No element
The relevant error-causing widget was:
Obx Obx:file:///Users/elamparithibalakrishnan/workspace/purple_star/lib/widgets/cart_total_value.dart:14:12
When the exception was thrown, this was the stack:
#0 ListMixin.reduce (dart:collection/list.dart:222:22)
#1 CartController.total (package:purple_star/screens/cart/cart_controller.dart:40:8)
#2 CartTotalWidget.totalController (package:purple_star/widgets/cart_total_value.dart:34:19)
#3 CartTotalWidget.build.<anonymous closure> (package:purple_star/widgets/cart_total_value.dart:27:15)
#4 Obx.build (package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart:70:28)
#5 RxInterface.notifyChildren (package:get/get_rx/src/rx_types/rx_core/rx_interface.dart:26:27)
#6 _ObxState.build (package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart:54:19)
#7 StatefulElement.build (package:flutter/src/widgets/framework.dart:4782:27)
Thanks in advance.