尝试为FireStore中的项目创建地图,例如:
class Hero {
final int id;
String name;
bool pickupNeeded;
String url;
bool partPickup;
Hero(this.id, this.name, this.pickupNeeded, this.url, this.partPickup);
factory Hero.fromJson(Map<String, dynamic> hero) =>
Hero(_toInt(hero['id']), hero['name'], hero['pickupNeeded'], hero['url'], hero['partPickup']);
Map toJson() => {'id' : id, 'name': name, 'pickupNeeded' : pickupNeeded, 'url' : url, 'partPickup' : partPickup,};
int _toInt(id) => id is int ? id : int.parse(id);
但是,_toInt in the hero(_toInt(hero['id']
...给出错误:“无法从工厂构造函数访问实例成员”
我想念什么?他们的GitHub sample
上的AngularDart英雄之旅就是这样。
hero_service获得英雄呼叫:
Future<Hero> get(dynamic id) async {
try {
return Hero.fromJson(id);
} catch (e) {
throw _handleError(e);
}
}
以及正在调用它的Hero组件:
void onActivate(_, RouterState current) async {
final id = RoutePaths().getId(current.parameters);
if (id != null) hero = await (_heroService.get(id));
}
答案 0 :(得分:0)
将_toInt
设为静态,您会很高兴。
static int _toInt(id) => id is int ? id : int.parse(id);