We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
改为
var temp=cartString==null?[]:json.decode(cartString.toString()); //把获得值转变成List List<Map> tempList= (temp as List).cast(); // 如果数据不为空,那么需要给 cartInfoList 添加数据 if (tempList != null && tempList.length > 0) { tempList.forEach((item) { cartList.add(new CartInfoModel.fromJson(item)); }); } //声明变量,用于判断购物车中是否已经存在此商品ID var isHave= false; //默认为没有 int ival=0; //用于进行循环的索引使用 tempList.forEach((item){//进行循环,找出是否已经存在该商品 //如果存在,数量进行+1操作 if(item['goodsId']==goodsId){ tempList[ival]['count']=item['count']+1; //关键代码-----------------start cartList[ival].count++; //关键代码-----------------end isHave=true; } ival++; });
我的项目的代码
import 'package:flutter/material.dart'; import 'package:flutter_shop_mall/model/cartInfoModel.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'dart:convert'; class CartProvide extends ChangeNotifier { String cartGoodsStr = "[]"; List<CartInfoModel> cartInfoList = []; void save(String goodsId, String goodsName, int count, double presentPrice, String images) async { // 初始化SharedPreferences SharedPreferences prefs = await SharedPreferences.getInstance(); // 获取数据 cartGoodsStr = prefs.getString('cartInfo'); debugPrint('cartGoodsStr 值 $cartGoodsStr'); // 判断cartString是否为空,为空说明是第一次添加,或者被key被清除了。 // 如果有值进行decode操作 var temp = cartGoodsStr == null ? [] : json.decode(cartGoodsStr.toString()); List<Map> tampList = (temp as List).cast(); // 如果数据不为空,那么需要给 cartInfoList 添加数据 if (tampList != null && tampList.length > 0) { tampList.forEach((item) { cartInfoList.add(new CartInfoModel.fromJson(item)); }); } // 判断是否存在当前商品 bool isHaveThisGoods = false; // 用于进行循环的索引使用 int ival = 0; // 进行循环,找出是否已经存在该商品 tampList.forEach((item) { debugPrint('ival 前 $ival'); // 如果存在,数量进行+1操作 if (item['goodsId'] == goodsId) { debugPrint('有一样的数据,+1'); tampList[ival]['count'] = item['count'] + 1; cartInfoList[ival].count++; isHaveThisGoods = true; } ival++; }); debugPrint('ival 后 $ival'); // 如果没有,进行增加 if (!isHaveThisGoods) { Map<String, dynamic> newGoods = { 'goodsId': goodsId, 'goodsName': goodsName, 'count': count, 'price': presentPrice, 'images': images }; tampList.add(newGoods); cartInfoList.add(CartInfoModel.fromJson(newGoods)); } //把字符串进行encode操作, cartGoodsStr = json.encode(tampList).toString(); debugPrint('持久化数据 字符串 $cartGoodsStr'); debugPrint('持久化数据 列表 $cartInfoList'); prefs.setString('cartInfo', cartGoodsStr); //进行持久化 } void remove() async { SharedPreferences prefs = await SharedPreferences.getInstance(); //prefs.clear();//清空键值对 prefs.remove('cartInfo'); // 置空数据 cartInfoList = []; debugPrint('清空完成-----------------'); notifyListeners(); } // 得到购物车中的商品 getCartInfoGoods() async { SharedPreferences prefs = await SharedPreferences.getInstance(); var cartInfoStr = prefs.getString('cartInfo'); //把cartList进行初始化,防止数据混乱 cartInfoList = []; // 判断得到的字符串是否有值,如果不判断会报错 if (cartInfoStr == null) { cartInfoList = []; } else { var cartInfoJson = json.decode(cartInfoStr.toString()); List<Map> tampList = (cartInfoJson as List).cast(); tampList.forEach((item) { cartInfoList.add(new CartInfoModel.fromJson(item)); }); } notifyListeners(); } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
改为
我的项目的代码
The text was updated successfully, but these errors were encountered: