我遇到了一个奇怪的情况,Firestore期望使用我创建的对象,而不是我试图提供给add()函数的DocumentReference。
点击提交按钮,我叫 submitForm()
submitForm() {
// mockup of an order list
orderListInput = [{
'product': _ws.products.firstWhere((p) => p.id == 'i5ZLMIyT6gu7NcNkWNkB'), // a Product object
'quantity': 43,
'unit': 'st',
}];
deliveryListInput = orderListInput;
statusInput = 'approved';
if (true) { // TODO: do some check
_os.postItem(Order(
customerInput,
DateTime.now(),
deliveryDateInput,
orderListInput,
statusInput,
deliveryNoteInput,
deliveryListInput,
etaInput,
deliveredDateInput,
));
}
}
我的订单类文件:
library mango.order;
import 'package:firebase/firebase.dart' as fb;
import 'customer.dart';
import 'product.dart';
const String jsonTagId = 'id';
const String jsonTagCustomer = 'customer';
const String jsonTagDateAdded = 'dateAdded';
const String jsonTagDeliveryDate = 'deliveryDate';
const String jsonTagOrderList = 'orderList';
const String jsonTagStatus = 'status';
const String jsonTagETA = 'eta';
const String jsonTagDeliveryNote = 'deliveryNote';
const String jsonTagDeliveryList = 'deliveryList';
const String jsonTagDeliveredDate = 'deliveredDate';
class Order {
String id;
Customer customer;
DateTime dateAdded;
DateTime deliveryDate;
List<Map<String, dynamic>> orderList;
DateTime eta;
String status;
String deliveryNote;
List<Map<String, dynamic>> deliveryList;
DateTime deliveredDate;
Order(
this.customer,
this.dateAdded,
this.deliveryDate,
this.orderList,
this.status, [
this.deliveryNote,
this.deliveryList,
this.eta,
this.deliveredDate,
this.id,
]);
static Map toMap(Order item) {
item.orderList.forEach((e) {
e['product'] = fb.firestore().doc((e['product'] as Product).path);
});
item.deliveryList.forEach((e) {
e['product'] = fb.firestore().doc((e['product'] as Product).path);
});
Map<String, dynamic> order = {
jsonTagCustomer: fb.firestore().doc(item.customer.path),
jsonTagDateAdded: item.dateAdded,
jsonTagDeliveryDate: item.deliveryDate,
jsonTagOrderList: item.orderList,
jsonTagStatus: item.status,
jsonTagDeliveryNote: item.deliveryNote,
jsonTagDeliveryList: item.deliveryList,
jsonTagETA: item.eta,
jsonTagDeliveredDate: item.deliveredDate,
};
return order;
}
}
我的产品类文件:
library mango.product;
const String jsonTagId = 'id';
const String jsonTagName = 'name';
const String jsonTagQuantity = 'quantity';
const String jsonTagDefaultUnit = 'defaultUnit';
class Product {
String id;
String path;
String name;
num quantity;
String defaultUnit;
Product(this.name, this.quantity, this.defaultUnit, [this.id, this.path]);
static Map toMap(Product item) {
Map<String, dynamic> product = {
jsonTagName: item.name,
jsonTagQuantity: item.quantity,
jsonTagDefaultUnit: item.defaultUnit,
};
return product;
}
}
和OrderService的postItem方法这样做:
postItem(Order item) async {
try {
await _rootRef.add(Order.toMap(item));
}
catch (err) {
print('Error adding Order to database: $err');
}
}
}
在运行时获取此错误:
Error adding Order to database: Type 'DocumentReference' is not a subtype of expected type 'Product'.
指的是 orderListInput 和 deliveryListInput ,它们的结构都如下:
[
{
'product': DocumentReference,
'quantity': num,
'unit': String,
},
...
]
将orderListInput [0] ['product']更改为产品类型,显然,它会抱怨很多,因为他们不知道如何进行转换。
我什么都不知道。 希望我已经充分描述了这个问题。
预先感谢