EN VI

Flutter - Exception caught by gesture: type 'double' is not a subtype of type 'Double' in type cast?

2024-03-12 14:30:05
Flutter - Exception caught by gesture: type 'double' is not a subtype of type 'Double' in type cast

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

Solution:

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);
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login