1

// I am trying this code and it is not adding to the 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();
  });
}

// it is giving these errors

The following _TypeError was thrown while handling a gesture: type 'List' is not a subtype of type 'String'

When the exception was thrown, this was the stack: #0 SharedPreferences.getString (package:shared_preferences/shared_preferences.dart:98:35) #1 checkItemInCart (package:e_shop/Store/storehome.dart:331:12) #2 sourceInfo. (package:e_shop/Store/storehome.dart:301:31) #3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19) #4 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:1111:38) ... Handler: "onTap" Recognizer: TapGestureRecognizer#47ccb debugOwner: GestureDetector state: ready won arena finalPosition: Offset(380.3, 334.6) finalLocalPosition: Offset(22.8, 31.6) button: 1 sent tap down

3
  • could you tell me where this error shows up? in which line? Commented Mar 14, 2021 at 15:45
  • that is the whole error it is not telling me at which line. Commented Mar 15, 2021 at 11:32
  • It's saying that error in 98th line Commented Mar 15, 2021 at 15:12

2 Answers 2

1

the problem is in .getString(EcommerceApp.userCartList) you have to set an index to it because EcommerceApp.userCartList returns a list use it something like this

.getString(EcommerceApp.userCartList[0])
Sign up to request clarification or add additional context in comments.

3 Comments

I tried it and it is giving me this error:
The method 'contains' was called on null. Receiver: null Tried calling: contains("Phone : Samsung Galaxy note 10 12GB RAM 128GB")
it is because EcommerceApp.userCartList[0] returns "Phone : Samsung Galaxy note 10 12GB RAM 128GB" this value and there is no value in the EcommerceApp.sharedpreferences with that name so check if the name that you stored and trying to get is the same.
0

thanks all for helping. all I needed to do is change the getString to getStringList

an now it is working.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.