我正在尝试在Future构建器内的ListView中使用多维json显示用户详细信息。我有从https://jsonplaceholder.typicode.com/users?id=1获得的这些json数据。这些是json代码:
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}
]
我尝试通过https://app.quicktype.io/生成模型,然后得到了这个数据模型:
// To parse this JSON data, do
//
// final user = userFromJson(jsonString);
import 'dart:convert';
List<User> userFromJson(String str) => new List<User>.from(json.decode(str).map((x) => User.fromJson(x)));
String userToJson(List<User> data) => json.encode(new List<dynamic>.from(data.map((x) => x.toJson())));
class User {
int id;
String name;
String username;
String email;
Address address;
String phone;
String website;
Company company;
User({
this.id,
this.name,
this.username,
this.email,
this.address,
this.phone,
this.website,
this.company,
});
factory User.fromJson(Map<String, dynamic> json) => new User(
id: json["id"],
name: json["name"],
username: json["username"],
email: json["email"],
address: Address.fromJson(json["address"]),
phone: json["phone"],
website: json["website"],
company: Company.fromJson(json["company"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"username": username,
"email": email,
"address": address.toJson(),
"phone": phone,
"website": website,
"company": company.toJson(),
};
}
class Address {
String street;
String suite;
String city;
String zipcode;
Geo geo;
Address({
this.street,
this.suite,
this.city,
this.zipcode,
this.geo,
});
factory Address.fromJson(Map<String, dynamic> json) => new Address(
street: json["street"],
suite: json["suite"],
city: json["city"],
zipcode: json["zipcode"],
geo: Geo.fromJson(json["geo"]),
);
Map<String, dynamic> toJson() => {
"street": street,
"suite": suite,
"city": city,
"zipcode": zipcode,
"geo": geo.toJson(),
};
}
class Geo {
String lat;
String lng;
Geo({
this.lat,
this.lng,
});
factory Geo.fromJson(Map<String, dynamic> json) => new Geo(
lat: json["lat"],
lng: json["lng"],
);
Map<String, dynamic> toJson() => {
"lat": lat,
"lng": lng,
};
}
class Company {
String name;
String catchPhrase;
String bs;
Company({
this.name,
this.catchPhrase,
this.bs,
});
factory Company.fromJson(Map<String, dynamic> json) => new Company(
name: json["name"],
catchPhrase: json["catchPhrase"],
bs: json["bs"],
);
Map<String, dynamic> toJson() => {
"name": name,
"catchPhrase": catchPhrase,
"bs": bs,
};
}
我需要在Future Builder的ListView中显示此数据。我该怎么做呢?请有人帮我吗?
答案 0 :(得分:0)
此响应使用http dio lib。
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: FutureBuilder(
future: _getData(),
builder: (_, AsyncSnapshot<User> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return new Theme(
data: Theme.of(context).copyWith(accentColor: Colors.blue),
child: new CircularProgressIndicator(),
);
}
if (!snapshot.hasData) {
return Center(
child: Text("No data found"),
);
}
User user = snapshot.data;
return ListView(
children: <Widget>[
ListTile(
title: Text(user.name),
subtitle: Text(user.email),
),
],
);
},
),
),
);
}
Future<User> _getData() async {
final Response response =
await dio.get("https://jsonplaceholder.typicode.com/users?id=1");
return User.fromJson(response.data[0]);
}