Home > Enterprise >  The argument type 'OrderItem' can't be assigned to the parameter type 'OrderItem
The argument type 'OrderItem' can't be assigned to the parameter type 'OrderItem

Time:01-08

I am unable to assign my one class to another even though I have imported the lib for them. I am following a course . In this I will fetch the orders from firebase and showing the orderscreen.

Orderscreen(where i am getting the error):

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

import '../providers/orders.dart' show Orders;

import '../widgets/app_drawer.dart';
import 'package:shop_app/Widgets/OrderItem.dart';

class OrdersScreen extends StatelessWidget {
  static const routeName = '/orders';

  @override
  Widget build(BuildContext context) {
    print('building orders');
    // final orderData = Provider.of<Orders>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('Your Orders'),
      ),
      drawer: AppDrawer(),
      body: FutureBuilder(
        future: Provider.of<Orders>(context, listen: false).fetchandSetOrders(),
        builder: (ctx, dataSnapshot) {
          if (dataSnapshot.connectionState == ConnectionState.waiting) {
            return Center(child: CircularProgressIndicator());
          } else {
            if (dataSnapshot.error != null) {
              // ...
              // Do error handling stuff
              return Center(
                child: Text('An error occurred!'),
              );
            } else {
              return Consumer<Orders>(
                builder: (ctx, orderData, child) => ListView.builder(
                      itemCount: orderData.orders.length,
                      itemBuilder: (ctx, i) => OrderItem(orderData.orders[i])//getting error here
                    ),
              );
            }
          }
        },
      ),
    );
  }
}

My orderitem class :

class OrderItem extends StatefulWidget {
  final ord.OrderItem order;

  OrderItem(this.order);

  @override
  State<OrderItem> createState() => _OrderItemState();
}

class _OrderItemState extends State<OrderItem> {
  var _expanded = false;
  @override
  Widget build(BuildContext context) {
    return Card(
      margin: EdgeInsets.all(10),
      child: Column(
        children: [
          ListTile(
              title: Text('\$${widget.order.amount}'),
              subtitle: Text(
                DateFormat('dd/MM/yyyy/ hh:mm').format(widget.order.dateTime),
              ),
              trailing: IconButton(
                icon: Icon(_expanded
                    ? Icons.expand_less_rounded
                    : Icons.expand_more_rounded),
                onPressed: () {
                  setState(() {
                    _expanded = !_expanded;
                  });
                },
              )),
          if (_expanded)
            Container(
              height: min(widget.order.products.length * 20.0   100, 180),
              child: ListView(
                children: widget.order.products
                    .map((prod) => Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(prod.title),
                            Text('${prod.quanitity}x \$${prod.price}')
                          ],
                        ))
                    .toList()

                //min function will give min of two functions , if expanded is true then this will take place
                ,
              ),
            ),
        ],
      ),
    );
  }
}

My orders class:

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

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

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

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

Thanks guys for all your help. it would be helpful if you can explain why i got this error as I am a novice to flutter

CodePudding user response:

Your widgets file name and data class name are same.It has same class name.Make data class name and widgets name different.It will solve the issue

  •  Tags:  
  • Related