1

// 我正在尝试这段代码,但它没有添加到 firebase

void checkItemInCart(String shortInfoAsId, BuildContext context) {
  EcommerceApp.sharedPreferences
          .getString(EcommerceApp.userCartList)
          .contains(shortInfoAsId)
      ? Fluttertoast.showToast(msg: "Item already in Cart.")
      : addItemToCart(shortInfoAsId, context);
}

addItemToCart(String shortInfoAsId, BuildContext context) {
  List tempCartList =
      EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList);
  tempCartList.add(shortInfoAsId);

  EcommerceApp.firestore.collection(EcommerceApp.collectionUser)
      .document(EcommerceApp.sharedPreferences.getString(EcommerceApp.userUID))
      .updateData({
    EcommerceApp.userCartList: tempCartList,
  }).then((v){
    Fluttertoast.showToast(msg: "Item Added to Cart Successfully");

    EcommerceApp.sharedPreferences.setStringList(EcommerceApp.userCartList, 
tempCartList);

    Provider.of<CartItemCounter>(context, listen: false).displayResult();
  });
}

// 它给出了这些错误

处理手势时引发以下 _TypeError:类型“列表”不是“字符串”类型的子类型

抛出异常时,这是堆栈:#0 SharedPreferences.getString (package:shared_preferences/shared_preferences.dart:98:35) #1 checkItemInCart (package:e_shop/Store/storehome.dart:331:12) #2 sourceInfo . (包:e_shop/Store/storehome.dart:301:31)#3 _InkResponseState._handleTap(包:flutter/src/material/ink_well.dart:993:19)#4 _InkResponseState.build。(包:flutter/src/material/ink_well.dart:1111:38)...处理程序:“onTap”识别器:TapGestureRecognizer#47ccb debugOwner:GestureDetector 状态:准备好赢得竞技场finalPosition:Offset(380.3,334.6)finalLocalPosition:Offset( 22.8, 31.6) 按钮:1 发送点击向下

4

2 回答 2

1

问题出在 .getString(EcommerceApp.userCartList) 中,您必须为其设置索引,因为 EcommerceApp.userCartList 返回一个列表,使用它像这样

.getString(EcommerceApp.userCartList[0])
于 2021-03-14T15:48:35.110 回答
0

感谢大家的帮助。我需要做的就是将 getString 更改为 getStringList

现在它正在工作。

于 2021-03-15T12:08:37.110 回答