我想将我的对象转换为JSON,所以我实现了以下代码。
import "package:behoove/models/product.dart";
class Order {
Product _product;
int _quantity;
int _id;
Order(this._product, this._quantity, this._id);
int get id => _id;
int get quantity => _quantity;
Product get product => _product;
double get orderPrice => _quantity * double.parse(_product.discountedPrice ?? _product.price);
Map<String, dynamic> toJson() => {
"id": _product.id.toString(),
"name": _product.name,
"price": _product.price,
"quantity": _quantity.toString(),
"attributes": {},
"conditions": []
};
}
应用程序返回的JSON数据为:
{id: 9, name: GoldStar Classic 032, price: 1200, quantity: 1, attributes: {}, conditions: []}}
但是在DartPad上的JSON则为:
{"id":"1","name":"Sabin","price":200,"quantity":3,"attributes":{},"conditions":[]}
我该如何在我的应用程序上获得相同的输出,请帮忙。另外为什么DartPad和应用程序的输出不相似?