skip to Main Content

When I click on add to cart, I get an error after debugging and running the code.
price is defined in the file named product.dart like final double price and in products.dart for 4 products I have separate pricing 49.99, 59.99, ….

enter image description here

here are my codes:

import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:my_shop/providers/cart.dart';
import 'package:my_shop/providers/product.dart';

import 'package:my_shop/screens/product_detail_screen.dart';
import 'package:provider/provider.dart';

class ProductItem extends StatelessWidget {

@override
Widget build(BuildContext context) {
final product = Provider.of<Product>(context, listen: false);
final cart = Provider.of<Cart>(context, listen: false);
return ClipRRect(
  borderRadius: BorderRadius.circular(10),
  child: GridTile(
    footer: GridTileBar(
        title: Text(
          product.title,
          textAlign: TextAlign.center,
          textScaler: const TextScaler.linear(2 / 3),
        ),
        backgroundColor: Colors.black87,
        leading: Consumer<Product>(
            builder: (ctx, product, _) => IconButton(
                  icon: Icon(product.isFavorite
                      ? Icons.favorite
                      : Icons.favorite_border),
                  color: Theme.of(context).colorScheme.secondary,
                  onPressed: () {
                    product.toggleFavoritestatus();
                  },
                )),
        trailing: IconButton(
          icon: const Icon(Icons.shopping_cart),
          color: Theme.of(context).colorScheme.secondary,
          onPressed: () {
            cart.addItem(
                product.id, product.price as Double, product.title);
            ScaffoldMessenger.of(context).showSnackBar(SnackBar(
              content: Text('The item added to your cart!'),
              duration: Duration(seconds: 2),
              action: SnackBarAction(label: 'UNDO', onPressed: () {}),
            ));
          },
        )),
    child: GestureDetector(
      onTap: () {
        Navigator.of(context).pushNamed(
          ProductDetailScreen.routeName,
          arguments: product.id,
        );
      },
      child: Image.network(
        product.imageUrl,
        fit: BoxFit.cover,
      ),
    ),
  ),
);
}
}

Please help me out and let me know how to fix it.
thanks in advance

2

Answers


  1. I think you have mistakenly written Double instead of double change this line of code

    cart.addItem(product.id, product.price as Double, product.title);
    

    to this

    cart.addItem(product.id, product.price as double, product.title);
    
    Login or Signup to reply.
  2. This is happening as you are using your own custom Double class for casting the value of product.price over here:

    cart.addItem(product.id, product.price as Double, product.title);

    Dart has floating point values as double, NOT Double. The data coming from the db is also coming as a double and therefore is throwing an exception when you are already trying to cast an already pre-define data type as something else.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search