Home > front end >  I keep getting the error argument type 'List <CartModel>' can't be assigned to
I keep getting the error argument type 'List <CartModel>' can't be assigned to

Time:02-20

I am new to flutter and taking Maximillians flutter course, but i am stuck in passing an argument of list to a parameter of list type . I can't see anything wrong with the code so far. The code

import 'package:flutter/foundation.dart';

import '/providers/cart_provider.dart';
import 'package:project_2/widgets/cart_item.dart';

class OrderItem {
  final String id;
  final double amount;
  final List<CartItem> products;
  final DateTime dateTime;

  OrderItem({
    required this.id,
    required this.amount,
    required this.products,
    required this.dateTime,
  });
}

class Orders with ChangeNotifier {
  final List<OrderItem> _orders = [];

  List<OrderItem> get orders {
    return [..._orders];
  }

  void addOrder(List<CartItem> cartProducts, double total) {
    _orders.insert(
      0,
      OrderItem(
        id: DateTime.now().toString(),
        amount: total,
        dateTime: DateTime.now(),
        products: cartProducts,
      ),
    );
    notifyListeners();
  }
}

The provider class above is used in the screen where i get the error. This is shown below

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '/providers/cart_provider.dart';
import 'package:project_2/providers/order_provider.dart';

import '/widgets/cart_item.dart';

class CartScreen extends StatelessWidget {
  const CartScreen({Key? key}) : super(key: key);

  static const cartRoute = '/Cart Screen';

  @override
  Widget build(BuildContext context) {
    final cartvalues = Provider.of<CartProvider>(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text('Cart Screen'),
        elevation: 0.5,
      ),
      body: Column(
        children: <Widget>[
          Card(
            margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Container(
                  child: const Padding(
                    padding: EdgeInsets.all(20.0),
                    child: Text(
                      'Total',
                      style: TextStyle(fontSize: 20),
                    ),
                  ),
                ),
                Spacer(),
                Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: Chip(
                    label: Text(
                        '\$${cartvalues.totalAmount.toStringAsFixed(2)}',
                        style: TextStyle(fontSize: 13)),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: ElevatedButton(
                    onPressed: () {
                      Provider.of<Orders>(context, listen: false).addOrder(
                        cartvalues.items.values.toList(),
                        cartvalues.totalAmount,
                      );
                      cartvalues.clear();
                    },
                    child: Text('Order'),
                  ),
                )
              ],
            ),
          ),
          SizedBox(),
          Expanded(
            child: ListView.builder(
              itemCount: cartvalues.itemCount,
              itemBuilder: (ctx, index) => CartItem(
                cartvalues.items.keys.toList()[index],
                cartvalues.items.values.toList()[index].id,
                cartvalues.items.values.toList()[index].price,
                cartvalues.items.values.toList()[index].quantity,
                cartvalues.items.values.toList()[index].title,
              ),
            ),
          )
        ],
      ),
    );
  }
}

The cart Item widget code is also shown below

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'package:project_2/providers/cart_provider.dart';

class CartItem extends StatelessWidget {
  final String productId;
  final String id;
  final double price;
  final int quantity;
  final String title;

  CartItem(
    this.productId,
    this.id,
    this.price,
    this.quantity,
    this.title,
  );
  @override
  Widget build(BuildContext context) {
    return Dismissible(
      key: ValueKey(id),
      background: Container(
        child: Icon(Icons.delete),  
        decoration: BoxDecoration(
          color: Colors.redAccent,
          borderRadius: BorderRadius.circular(4)),
        alignment: Alignment.centerRight,
        padding: EdgeInsets.only(right: 10),
        margin: EdgeInsets.symmetric(horizontal: 15, vertical: 4,)
      ),
      direction: DismissDirection.endToStart,
      onDismissed: (direction) {
        Provider.of<CartProvider>(context, listen: false).removeItem(productId);
      },
      child: Card(
        margin: EdgeInsets.symmetric(horizontal: 15, vertical: 4,),
        child: ListTile(
          leading: CircleAvatar(
            child: FittedBox(child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text('\$$price'),
            ))
          ),
          title: Text(title),
          subtitle: Text('${(price * quantity).toStringAsFixed(2)}'),
          trailing: Text('${quantity}x'),
        ),
      ),
    );
  }
}

CodePudding user response:

Bro you should avoid using multiple providers. if your app is too big and you can split it into two or more Modules (Parts), then you can use multiple providers. else just declate a class provider for the whole app and use it. also, we don't use ChangeNotifier for models. I can't see a cartProvider class in your questions, please give us more insight into your problem.

  • Related