[英]Flutter Riverpod StateNotifier to not add to end of List
im new to Riverpod and Flutter the problem i'm having is that when i use StateNotifier, the state is immutable so i have to create new state and add new value to the end of List我是 Riverpod 和 Flutter 的新手,我遇到的问题是当我使用 StateNotifier 时,state 是不可变的,所以我必须创建新的 state 并将新值添加到列表的末尾
void add(Cart cart) {
state = [...state, cart];
}
void inQuan(int i) {
final tempCart = state[i];
tempCart.quantity++;
remove(state[i]);
add(tempCart);
}
and this makes my list pushes the item i'm editing to the end is there any way to optimize my code?这使我的列表将我正在编辑的项目推到最后有什么方法可以优化我的代码吗?
This creates only one new array.这只会创建一个新数组。
void inQuan(int i) {
state = [
for (int index = 0; i < state.length; index++)
if (i == index)
state[i].copyWith(quantity: state[i].quantity++)
else
state[i]
];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.