【问题标题】:How to iterate this data model in ListView inside Future Builder?如何在 Future Builder 内的 ListView 中迭代此数据模型?
【发布时间】:2019-10-13 05:16:12
【问题描述】:

我正在尝试在 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 中显示这些数据。我该怎么做呢?请有人帮帮我?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    此响应使用 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]);
    }
    

    【讨论】:

    • 感谢您的回答,我很好奇如何获取父数据下方第三步的纬度和经度数据?
    猜你喜欢
    • 2021-06-17
    • 2021-03-02
    • 2022-01-24
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    相关资源
    最近更新 更多