尝试为 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']...给出错误:“无法从工厂构造函数访问实例成员”
我错过了什么?AngularDart Tour of Heroes 在他们的GitHub sample.
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));
}