当从firebase collection中的字段获取数据时,我得到这个错误该字段是用户订单
的数组。期望一个类型为'Widget'的值,但得到一个类型为' mappedlisttiterable <dynamic,Widget> '
StreamBuilder<dynamic>(
stream: FirebaseFirestore.instance.collection('users').snapshots(),
builder: (context, AsyncSnapshot snapshot) {
//print(snapshot);
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.connectionState == ConnectionState.active) {
if (snapshot.data == null) {
return Center(
child: Padding(
padding: const EdgeInsets.only(top: 30),
child: ReusibleText(text: 'Your store is empty'),
));
} else if (snapshot.hasData) {
return Container(
child: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
// final List orders=[];
// for(var order in snapshot.data[index]['orders']){
// orders.add(order);
// }
return snapshot.data.docs[index]['orders']
.map<Widget>((order) => Column(
children: [
AllOrdersWidget(
userId: order['userId'],
productId: order['productId'],
userName: order['userName'],
imageUrl: order['imageUrl'],
quantity: int.parse(order['quantity']),
date: order['orderDate'],
address: order['address'],
orderId: order['orderId'],
price: order['price'],
totalPrice: order['totalPrice'],
),
Divider(),
],
));
}),
);
} else {
return ReusibleText(text: "Error");
}
}
return ReusibleText(text: "Something went wrong");
},
),
您必须通过调用.toList()
将您的MappedListIterable
转换为List
。然后您将收到Expected a value of type 'Widget', but got one of type 'List<Widget>
,因为您的.map
返回Column
的List
,这是一个列表,当您应该返回单个Widget
时。要解决这个问题,您可以将return更改为:
return Column( // <-- Wrap your map with a Column and use the spread operator to insert all the map elements into the Column children.
children: [
...snapshot.data.docs[index]['orders'].map<Widget>(
(order) => ...[
AllOrdersWidget(
userId: order['userId'],
productId: order['productId'],
userName: order['userName'],
imageUrl: order['imageUrl'],
quantity: int.parse(order['quantity']),
date: order['orderDate'],
address: order['address'],
orderId: order['orderId'],
price: order['price'],
totalPrice: order['totalPrice'],
),
Divider(),
],
).toList(), // <-- Add the toList()
],
);