'The argument type 'OrderItem' can't be assigned to the parameter type 'OrderItem'
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
Solution 1:[1]
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
Solution 2:[2]
Change the file name and widget name is working
Plugin Use: provider: ^6.0.2
OrdersScreen (Order_screen.dart)
import 'package:flutter/material.dart';
import 'package:order_item_proj/OrderItem.dart';
import 'package:order_item_proj/Orders.dart' show Orders;
import 'package:provider/provider.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'),
),
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) => OrderItemWidget(orderData.orders[i])),
);
}
}
},
),
);
}
}
OrderItem Widget (OrderItem.dart)
import 'package:flutter/material.dart';
import 'package:order_item_proj/Orders.dart' as ord;
class OrderItemWidget extends StatefulWidget {
final ord.OrderItem order;
OrderItemWidget(this.order);
@override
State<OrderItemWidget> createState() => _OrderItemWidgetState();
}
class _OrderItemWidgetState extends State<OrderItemWidget> {
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(""),
trailing: IconButton(
icon: Icon(_expanded ? Icons.expand_less_rounded : Icons.expand_more_rounded),
onPressed: () {
setState(() {
_expanded = !_expanded;
});
},
)),
if (_expanded)
Container(
child: ListView(
children: widget.order.products
.map((prod) => Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("title"),
],
))
.toList()
//min function will give min of two functions , if expanded is true then this will take place
,
),
),
],
),
);
}
}
OrderItem Class (Orders.dart)
import 'package:flutter/cupertino.dart';
class OrderItem {
final String id;
final double amount;
final List<String> 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];
}
fetchandSetOrders() {}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Khadga shrestha |
Solution 2 | Suraj Rao |