Search code examples
flutterparse-platform

Save Pointer Flutter Parse


I am trying to save a "pointer" using Parse SDK for Flutter and am getting the error "RangeError: Maximum call stack size exceeded" in the debug console. Due to some research I know that error usually indicates a function called within a function for a never-ending loop.

Below is the Row() that holds the addToCart() function (of which is breaking the code). The FutureBuilder widget is used to tell if the user is currently logged in and send them to the login screen if not. Otherwise they can add an item to their cart. I need to add a pointer to the Products class using productID in the Cart table.

The cart table is set up to receive a pointer parseObject data type, as if I replace ..set('productID',(ParseObject('Products')..objectId = ParseObject("Products").objectId)) with ..set('productID',widget.productID) I get an error that says "wrong data type for parseObject pointer" (paraphrasing).

Lastly, If I change the data type from pointer to just a string and place in a string value, the code works and I can save records to the database. So I am not exactly sure why my pointer saving is not working.

            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                FutureBuilder(
                    future: ParseUser.currentUser(),
                    builder: ((context, snapshot) {
                      if (snapshot.hasData) {
                        return ElevatedButton(
                          onPressed: () => addToCart(),
                          style: ElevatedButton.styleFrom(
                              primary: const Color.fromARGB(255, 255, 203, 59),
                              onPrimary: Colors.black),
                          child: const Text("Add to Cart"),
                        );
                      } else {
                        return ElevatedButton(
                          onPressed: () {
                            Navigator.pushNamed(context, '/LogIn');
                          },
                          style: ElevatedButton.styleFrom(
                              primary: const Color.fromARGB(255, 255, 203, 59),
                              onPrimary: Colors.black),
                          child: const Text("Add to Cart"),
                        );
                      }
                    })),
                ElevatedButton(
                  onPressed: () {
                    Navigator.pushNamed(context, '/products/',
                        arguments: widget.productId);
                  },
                  style: ElevatedButton.styleFrom(
                      primary: const Color.fromARGB(255, 255, 203, 59),
                      onPrimary: Colors.black),
                  child: const Text("Product Details"),
                ),
              ],
            )
  Future<void> addToCart() async {
    ParseUser currentUser = await ParseUser.currentUser() as ParseUser;
    final addToCart = ParseObject('Cart')
      ..set(
          'productID',
          (ParseObject('Products')
            ..objectId = ParseObject("Products").objectId))
      ..set('userID', currentUser.objectId)
      ..set('quantity', qtyDropdownValue)
      ..set('subtotal',
          widget.productPrice.toDouble() * qtyDropdownValue.toDouble())
      ..set('productSize', sizeDropdownValue);
    await addToCart.save();
  }

Solution

  • I was able to work this out for any one who looks at this in the future.

    Here is the working code.

      Future<void> addToCart() async {
        ParseUser currentUser = await ParseUser.currentUser() as ParseUser;
        var addToCart = ParseObject('Cart')
          ..set(
              'productID',
              (ParseObject("Products")..objectId = widget.productObjectId)
                  .toPointer())
          ..set('userID', currentUser.objectId)
          ..set('quantity', qtyDropdownValue)
          ..set('subtotal',
              widget.productPrice.toDouble() * qtyDropdownValue.toDouble())
          ..set('productSize', sizeDropdownValue);
        await addToCart.save();
        showSuccess("Item Added to Cart!");
      }
    

    The showSuccess function at the end is an added function that doesn't need to be there. But as you can see, I really just needed to add a variable that defines the objectId for the object that I was trying to add to the cart. In my earlier code, I was not defining the specific object but instead just pointing it towards the Products table in general.