【发布时间】:2021-10-12 11:48:49
【问题描述】:
我正在观看旧的颤振课程,因为我真的找不到新的课程。但是由于 Flutter 是不断更新的,所以一旦你从旧课程中学习,它就会变得更具挑战性。
这是我的问题:
- fromObject 还在这里还是刚改成 fromJson?
- 您有模型样品吗?
- 我怎样才能使我的 _id 是唯一的?(使用 sql 等)
我正在尝试这样的事情,但在“Product.fromObject”中出现错误。
class Product {
int _id;
String _name;
String _description;
double _price;
Product(this._id, this._name, this._description, this._price);
Product.withId(this._id, this._name, this._description, this._price);
int get id => _id;
String get name => _name;
String get description => _description;
double get price => _price;
set name(String value) {
if (value.length >= 2) {
_name = value;
}
}
set description(String value) {
if (value.length >= 10) {
_description = value;
}
}
set price(double value) {
if (value > 0) {
_price = value;
}
}
Map<String, dynamic> toMap() {
var map = <String, dynamic>{};
map["name"] = _name;
map["description"] = _description;
map["price"] = _price;
map["id"] = _id;
return map;
}
Product.fromObject(dynamic o) {
_id = o["id"];
_name = o["name"];
_description = o["description"];
_price = o["price"];
}
}```
【问题讨论】:
标签: flutter