0

Good,

I have a problem with my application, since it generates an error when pressing the add to cart button, it generates an error and it is not added in firebase, I already checked that the variables have their name correctly, however I cannot find how solve this error, I appreciate all the help you can give me to solve this error!

void checkItemInCart(String shortInfoAsID, BuildContext context)
{
  EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList).contains(shortInfoAsID)
      ? Fluttertoast.showToast(msg: "El artículo ya existe en el carrito")
      : 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: "Artículo añadido al carrito");

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

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

The following NoSuchMethodError was thrown while handling a gesture:
The method 'collection' was called on null.
Receiver: null
Tried calling: collection("users")

When the exception was thrown, this was the stack: 
0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
1      addItemToCart (package:e_shop/Store/storehome.dart:283:26)
2      checkItemInCart (package:e_shop/Store/storehome.dart:276:9)
3      sourceInfo.<anonymous closure> (package:e_shop/Store/storehome.dart:246:27)
4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19)
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#696c2
  debugOwner: GestureDetector
  state: ready
  won arena
  finalPosition: Offset(338.5, 338.3)
  finalLocalPosition: Offset(32.5, 27.8)
  button: 1
  sent tap down
====================================================================================================
Bryan Soto
  • 19
  • 1
  • `EcommerceApp.firestore` is `null`. Since there is nothing in your code that sets it, it's up to you to figure out why it's null. Maybe you set it to null or maybe you did not set it at all. – nvoigt Feb 08 '21 at 07:33

1 Answers1

0

Declare this:

List tempCartList = List<>();  

Then do this :

  void checkItemInCart(String shortInfoAsID, BuildContext context)
        {
          EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList).contains(shortInfoAsID)
              ? Fluttertoast.showToast(msg: "El artículo ya existe en el carrito")
              : addItemToCart(shortInfoAsID, context);
        }
        
        addItemToCart(String shortInfoAsID, BuildContext context) {
          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: "Artículo añadido al carrito");
        
              EcommerceApp.sharedPreferences.setStringList(EcommerceApp.userCartList, tempCartList);
        
              Provider.of<CartItemCounter>(context, listen: false).displayResult();
          });
        }