Maroun Saade
Maroun Saade

Reputation: 11

Errors list<String> is not a subtype of type <String>

// 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

Upvotes: 1

Views: 274

Answers (2)

Maroun Saade
Maroun Saade

Reputation: 11

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

an now it is working.

Upvotes: 0

Biruk Telelew
Biruk Telelew

Reputation: 1193

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])

Upvotes: 1

Related Questions