Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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/OrderItem.dart';

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) =>
                        OrderItemview(orderData.orders[i])),  //this is where i am getting error
              );
            }
          }
        },
      ),
    );
  }
}


My orderitem class :

class OrderItemview extends StatefulWidget {
  final ord.OrderItems order;

  OrderItemview(this.order);

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

class _OrderItemState extends State<OrderItemview> {
  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 OrderItems {
  final String id;
  final double amount;
  final List<CartItem> products;
  final DateTime dateTime;

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

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

  List<OrderItems> 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

What I have tried:

I changed the class and widget name as before both with orderitem to avoid confusion but that has changed the error
Posted
Updated 8-Jan-22 6:42am

1 solution

The error is pretty explicit.

You're calling a function is is expecting an array or list of OrderItem objects and you're passing in a single OrderItem object.

Pass in an array of OrderItem objects, even if that array has only a single OrderItem in it.

In your code, I think you need to get rid of the "[i]":
Java
OrderItemview(orderData.orders)),
 
Share this answer
 
Comments
Kavya Bhargava 8-Jan-22 15:31pm    
Got rid of it but showing an error "The argument type 'List<orderitem>' can't be assigned to the parameter type 'OrderItems'."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900