- CastError(类型'*$_Order'不是类型强制转换中类型'Category'的子类型)
我的错误就在这里
var category = categories[index]
那是我的准则吗
class OrderWidget extends ConsumerWidget {
const OrderWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
return Container(
child: Column(children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: _odersList(ref),
)
]),
);
}
Widget _odersList(WidgetRef ref) {
final orders = ref.watch(
ordersProvider(
PaginationModel(page: 1, pageSize: 10),
),
);
return orders.when(
data: (list) {
return _buildOrderList(list!.cast<Order>(), list.cast<Category>(),
list.cast<Data>(), ref);
},
error: (_, __) => const Center(
child: Text("ERR"),
),
loading: () => const Center(
child: CircularProgressIndicator(),
));
}
Widget _buildOrderList(List<Order> orders, List<Category> categories,
List<Data> users, WidgetRef ref) {
return Container(
height: 500,
alignment: Alignment.centerLeft,
child: ListView.builder(
shrinkWrap: true,
physics: const ClampingScrollPhysics(),
scrollDirection: Axis.vertical,
itemCount: orders.length,
itemBuilder: (context, index) {
var order = orders[index];
var category = categories[index];
var user = users[index];
return Padding(
padding: EdgeInsets.all(8),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
order.orderName,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
Container(
width: double.infinity,
height: 120,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(5)),
color: Color.fromARGB(255, 212, 209, 206),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
child: SizedBox(
width: 100,
height: 100,
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
user.fullName,
style: TextStyle(fontSize: 13),
),
Text(
category.categoryName,
style: TextStyle(fontSize: 13),
),
Text(
"Ngày đặt bàn: 26/3/2023",
style: TextStyle(fontSize: 13),
),
Text(
"Ngày bắt đầu tiệc: 29/3/2023",
style: TextStyle(fontSize: 13),
),
Text(
order.orderDescription,
style: TextStyle(fontSize: 13),
),
],
),
Padding(
padding: const EdgeInsets.fromLTRB(45, 0, 0, 80),
child: Container(
width: 10,
height: 10,
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(30)),
color: Color.fromARGB(255, 1, 255, 1),
),
),
)
],
),
],
),
)
]),
);
},
),
);
}
}
1条答案
按热度按时间ubbxdtey1#
您的代码执行以下操作:
list!.cast<Order>()
、list.cast<Category>()
和list.cast<Data>()
不能全部成功,除非list
的每个元素都从Order
* 和 *Category
* 和 *Data
派生。Dart中的“转换”不转换数据;它不构造新对象。造型只改变对象的感知类型。
如果需要应用转换来创建新的
Order
、Category
和Data
元素,请使用List.map
或使用collection-for
。