您是否需要为 JSON 中的每个值映射一个工厂,或者只能获取我想要的值?
Do you need to Map a Factory for every value in a JSON or can grab only the values I want?
所以我正在研究这个 Dart & Flutter 代码实验室。
https://flutter.dev/docs/cookbook/networking/fetch-data
来自 https://jsonplaceholder.typicode.com/albums/1
class Album {
final int userId;
final int id;
final String title;
Album({
required this.userId,
required this.id,
required this.title,
});
factory Album.fromJson(Map<String, dynamic> json) {
return Album(
userId: json['userId'],
id: json['id'],
title: json['title'],
);
}
}
如果我只想要 userId,我可以忽略它吗?
我可以选择要从 jsonplaceholder 中获取哪些字段/值吗?
是的,您可以忽略 json
参数中的值。请记住,例如,如果您想忽略 title
,class 仍然有一个 title
字段。您将不得不以某种方式考虑到这一点。您可以从 class 中完全删除 title
字段,或者将该字段更改为可为空。
所以我正在研究这个 Dart & Flutter 代码实验室。 https://flutter.dev/docs/cookbook/networking/fetch-data
来自 https://jsonplaceholder.typicode.com/albums/1
class Album {
final int userId;
final int id;
final String title;
Album({
required this.userId,
required this.id,
required this.title,
});
factory Album.fromJson(Map<String, dynamic> json) {
return Album(
userId: json['userId'],
id: json['id'],
title: json['title'],
);
}
}
如果我只想要 userId,我可以忽略它吗?
我可以选择要从 jsonplaceholder 中获取哪些字段/值吗?
是的,您可以忽略 json
参数中的值。请记住,例如,如果您想忽略 title
,class 仍然有一个 title
字段。您将不得不以某种方式考虑到这一点。您可以从 class 中完全删除 title
字段,或者将该字段更改为可为空。