Return 来自 json 的多个对象与飞镖
Return multiple Objects from json with dart
在我正在编写的 Flutter 应用程序中,我是初学者,我想 return 来自 http 请求的 3 个对象。 http 请求工作正常,returns 数据如下:
{"one":{"fname":"Wyn","lname":"Owen","updated":1648076276673,"uid":"contact's uid","email":"wyn@wyn.com"},
"two":{"updated":1648076276673,"uid":"contact's uid","fname":"Roli","email":"roli@roli.com","lname":"Spider"},
"three":{"lname":"Poodle","email":"bill@bill.com","updated":1648076276673,"fname":"Bill","uid":"contact's uid"},
"user":{"name":null,"premium":false,"reg_id":"123456","created":1648076276673,"ads":true},
"channels":{"whatsapp":false,"email":true,"sms":false,"video":false,"app_msg":true},"shorts":{"expire":null,"code":"short_code"}}
它returns 5 KV 对。密钥将始终保持不变。我只对 3 个 KV 感兴趣:一、二和三。我想为每个 KV 对创建一个对象,并将它们称为一、二和三。我创建了以下模型:对象联系人包含 3 个人员对象:
class Contacts {
Contacts({
required this.one,
required this.two,
required this.three,
});
Person one;
Person two;
Person three;
factory Contacts.fromJson(Map<String, dynamic> json) => Contacts(
one: json["one"],
two: json["two"],
three: json["three"],
);
Map<String, dynamic> toJson() => {
"one": one.toJson(),
"two": two.toJson(),
"three": three.toJson(),
};
}
class Person {
Person({
required this.uid,
required this.updated,
required this.email,
required this.lname,
required this.fname,
});
String uid;
int updated;
String email;
String lname;
String fname;
factory Person.fromJson(Map<String, dynamic> json) => Person(
uid: json["uid"],
updated: json["updated"],
email: json["email"],
lname: json["lname"],
fname: json["fname"],
);
Map<String, dynamic> toJson() => {
"uid": uid,
"updated": updated,
"email": email,
"lname": lname,
"fname": fname,
};
}
这是class我写的:
class ContactsService {
Future<List<Person>> fetchPersons(String uid) async {
http.Response response =
await http.get(Uri.parse("$PersonURL?uid=$uid"));
if (response.statusCode == 200) {
Map ContactData = jsonDecode(response.body);
Person one = Person.fromJson(ContactData["one"]);
Person two = Person.fromJson(ContactData["two"]);
Person three = Person.fromJson(ContactData["three"]);
List Persons = [];
Persons.add(one);
Persons.add(two);
Persons.add(three);
return Persons;
} else {
throw Exception("Something has gone wrong, ${response.statusCode}");
}
}
}
为了在我的脚手架中获取对象,我会放置
future: ContactsService.fetchPersons()
我希望能够引用 one.fname、two.email 等 我想我在 fetchPersons() 中遗漏了一些东西。那么缺少什么?
TIA
你可以通过改变
来解决
Map ContactData = jsonDecode(response.body);
至
final contacts = Contacts.fromJson(jsonDecode(a));
和
Person one = Person.fromJson(ContactData["one"]);
Person two = Person.fromJson(ContactData["two"]);
Person three = Person.fromJson(ContactData["three"]);
至
final one = contacts.one;
final two = contacts.two;
final three = contacts.three;
作为补充,如果您使用 Google 提供的 JsonSerializable,则不需要序列化代码。
在我正在编写的 Flutter 应用程序中,我是初学者,我想 return 来自 http 请求的 3 个对象。 http 请求工作正常,returns 数据如下:
{"one":{"fname":"Wyn","lname":"Owen","updated":1648076276673,"uid":"contact's uid","email":"wyn@wyn.com"},
"two":{"updated":1648076276673,"uid":"contact's uid","fname":"Roli","email":"roli@roli.com","lname":"Spider"},
"three":{"lname":"Poodle","email":"bill@bill.com","updated":1648076276673,"fname":"Bill","uid":"contact's uid"},
"user":{"name":null,"premium":false,"reg_id":"123456","created":1648076276673,"ads":true},
"channels":{"whatsapp":false,"email":true,"sms":false,"video":false,"app_msg":true},"shorts":{"expire":null,"code":"short_code"}}
它returns 5 KV 对。密钥将始终保持不变。我只对 3 个 KV 感兴趣:一、二和三。我想为每个 KV 对创建一个对象,并将它们称为一、二和三。我创建了以下模型:对象联系人包含 3 个人员对象:
class Contacts {
Contacts({
required this.one,
required this.two,
required this.three,
});
Person one;
Person two;
Person three;
factory Contacts.fromJson(Map<String, dynamic> json) => Contacts(
one: json["one"],
two: json["two"],
three: json["three"],
);
Map<String, dynamic> toJson() => {
"one": one.toJson(),
"two": two.toJson(),
"three": three.toJson(),
};
}
class Person {
Person({
required this.uid,
required this.updated,
required this.email,
required this.lname,
required this.fname,
});
String uid;
int updated;
String email;
String lname;
String fname;
factory Person.fromJson(Map<String, dynamic> json) => Person(
uid: json["uid"],
updated: json["updated"],
email: json["email"],
lname: json["lname"],
fname: json["fname"],
);
Map<String, dynamic> toJson() => {
"uid": uid,
"updated": updated,
"email": email,
"lname": lname,
"fname": fname,
};
}
这是class我写的:
class ContactsService {
Future<List<Person>> fetchPersons(String uid) async {
http.Response response =
await http.get(Uri.parse("$PersonURL?uid=$uid"));
if (response.statusCode == 200) {
Map ContactData = jsonDecode(response.body);
Person one = Person.fromJson(ContactData["one"]);
Person two = Person.fromJson(ContactData["two"]);
Person three = Person.fromJson(ContactData["three"]);
List Persons = [];
Persons.add(one);
Persons.add(two);
Persons.add(three);
return Persons;
} else {
throw Exception("Something has gone wrong, ${response.statusCode}");
}
}
}
为了在我的脚手架中获取对象,我会放置
future: ContactsService.fetchPersons()
我希望能够引用 one.fname、two.email 等 我想我在 fetchPersons() 中遗漏了一些东西。那么缺少什么?
TIA
你可以通过改变
来解决Map ContactData = jsonDecode(response.body);
至
final contacts = Contacts.fromJson(jsonDecode(a));
和
Person one = Person.fromJson(ContactData["one"]);
Person two = Person.fromJson(ContactData["two"]);
Person three = Person.fromJson(ContactData["three"]);
至
final one = contacts.one;
final two = contacts.two;
final three = contacts.three;
作为补充,如果您使用 Google 提供的 JsonSerializable,则不需要序列化代码。