52.購物車_數據模型創建和Provide修改git
{"goodsId":"2171c20d77c340729d5d7ebc2039c08d","goodsName":"五糧液52°500ml","count":1,"price":830.0,"images":"http://images.baixingliangfan.cn/shopGoodsImg/20181229/20181229211422_8507.jpg"}
https://javiercbk.github.io/json_to_dart/github
model文件夾下新建類cartInfo.dartjson
類名叫作CartInfoModel。異步
這樣變量在add的時候,直接就能夠用上面定義的map的變量了async
把咱們的cartList打印在控制檯。ide
點擊運行報錯了this
model類的價格改爲double類型的spa
清空持久化數據的時候,也要把cartList這個新定義的List數據也清空一下。3d
model/cartInfo.dartcode
class CartInfoModel { String goodsId; String goodsName; int count; double price; String images; CartInfoModel( {this.goodsId, this.goodsName, this.count, this.price, this.images}); CartInfoModel.fromJson(Map<String, dynamic> json) { goodsId = json['goodsId']; goodsName = json['goodsName']; count = json['count']; price = json['price']; images = json['images']; } Map<String, dynamic> toJson() { final Map<String, dynamic> data = new Map<String, dynamic>(); data['goodsId'] = this.goodsId; data['goodsName'] = this.goodsName; data['count'] = this.count; data['price'] = this.price; data['images'] = this.images; return data; } }
provide/cart.dart
import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'dart:convert'; import '../model/cartInfo.dart'; class CartProvide with ChangeNotifier{ String cartString="[]";//聲明一個變量 作持久化的存儲 List<CartInfoModel> cartList=[]; //聲明一個異步的方法,購物車操做放在前臺不在請求後臺的數據 save(goodsId,goodsName,count,price,images) async { SharedPreferences prefs = await SharedPreferences.getInstance(); cartString= prefs.getString('cartInfo');//先從持久化中獲取 var temp = cartString==null?[]:json.decode(cartString.toString()); //聲明list 強制類型是Map List<Map> tempList=(temp as List).cast();//把temp轉成list bool isHave=false;//是否已經存在了這條記錄 int ival=0;//foreach循環的索引 //循環判斷列表是否存在該goodsId的商品,若是有就數量+1 tempList.forEach((item){ if(item['goodsId']==goodsId){ tempList[ival]['count']=item['count']+1; cartList[ival].count++; isHave=true; } ival++; }); //沒有不存在這個商品,就把商品的json數據加入的tempList中 if(!isHave){ Map<String,dynamic> newGoods={ 'goodsId':goodsId,//傳入進來的值 'goodsNmae':goodsName, 'count':count, 'price':price, 'images':images }; tempList.add(newGoods); cartList.add(CartInfoModel.fromJson(newGoods)); } cartString=json.encode(tempList).toString();//json數據轉字符串 print('字符串》》》》》》》》》》》${cartString}'); print('字符串》》》》》》》》》》》${cartList}'); prefs.setString('cartInfo', cartString); notifyListeners(); } remove() async{ SharedPreferences prefs=await SharedPreferences.getInstance(); prefs.remove('cartInfo'); cartList=[]; print('清空完成----------------------'); notifyListeners(); } getCartInfo() async{ SharedPreferences prefs=await SharedPreferences.getInstance(); cartString=prefs.getString('cartInfo');//持久化中得到字符串 cartList=[];//把最終的結果先設置爲空list if(cartString==null){ cartList=[];//若是持久化內沒有數據 那麼就仍是空的list }else{ //聲明臨時的變量 List<Map> tempList=(json.decode(cartString.toString()) as List).cast(); tempList.forEach((item){ cartList.add(CartInfoModel.fromJson(item));//json轉成對象,加入到cartList中 }); } notifyListeners();//通知 } }