class - 类的对象未在 Flutter 的循环中实例化

标签 class dart flutter

我正在使用 json.decode 将我的 JSON 数据转换为 User 类型的对象。我在 FutureBuilder 小部件中使用以下 Future。这是代码。

Future<List<User>> _getData() async {
    var data = await http.get("http://www.json-generator.com/api/json/get/cvAgrXxhOW?indent=2");
    var jsonBody = json.decode(data.body);

    List<User> users = [];

    for (var user in jsonBody) {
      print(user);
      User u = new User(user); 
      users.add(u);
    }

    print(users.length);

    return users;
}

类 User 如下所示。

class User {
  String _id;
  int index;
  String about;
  String name;
  String picture;
  String gender;
  int age;
  String registered;
  double longitude;
  String email;
  String eyeColor;
  String phone;
  String address;
  double latitude;
  String balance;
  String guid;
  String company;
  bool isActive;

  User(dynamic data){
    this._id = json.decode(data)["_id"];
    this.index = int.parse(json.decode(data)["index"]);
    this.about = json.decode(data)["about"];
    this.name = json.decode(data)["name"];
    this.picture = json.decode(data)["picture"];
    this.gender = json.decode(data)["gender"];
    this.age = int.parse(json.decode(data)["age"]);
    this.registered = json.decode(data)["registered"];
    this.longitude = double.parse(json.decode(data)["longitude"]);
    this.email = json.decode(data)["email"];
    this.eyeColor = json.decode(data)["eyeColor"];
    this.phone = json.decode(data)["phone"];
    this.address = json.decode(data)["address"];
    this.latitude = double.parse(json.decode(data)["latitude"]);
    this.balance = json.decode(data)["balance"];
    this.guid = json.decode(data)["guid"];
    this.company = json.decode(data)["company"];
    this.isActive = json.decode(data)["isActive"];
  }

  getUser(){
    return this;
  }
}

User u = new User(user); 行不会让代码进一步执行。 _getData() 函数中的 print 语句仅适用于循环的第一次迭代。在那之后,什么也没有发生。没有错误。

有什么建议吗?

最佳答案

你不需要再次解码数据

试试这个:

User(dynamic data){
    this._id = data["_id"];
    this.index = int.parse(data["index"]);
    this.about = data["about"];
    ....

已更新

var jsonBody = json.decode(data.body) as List;

最终更新 我发现了错误,您不需要解析为 int 或 double 它已经解析了,因为它是一个动态对象。

    User(Map<String, dynamic> data) {
      this._id = (data)["_id"];
      this.index = data["index"];
      this.about = (data)["about"];
      this.name = (data)["name"];

      this.picture = (data)["picture"];
      this.gender = (data)["gender"];
      this.age = (data)["age"];
      this.registered = (data)["registered"];
      this.longitude = data["longitude"];
      this.email = (data)["email"];
      this.eyeColor = (data)["eyeColor"];
      this.phone = (data)["phone"];
      this.address = (data)["address"];
      this.latitude = (data)["latitude"];
      this.balance = (data)["balance"];
      this.guid = (data)["guid"];
      this.company = (data)["company"];
      this.isActive = (data)["isActive"];
    }

关于class - 类的对象未在 Flutter 的循环中实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51754335/

相关文章:

flutter - 有没有更好的方法来将数据传递给没有状态的,继承的小部件的子级?

json - 如何获取JSON数据索引中的所有可用JSON数据

javascript - prototype.js 中的复制构造函数

Flutter Push 通知未在 IOS 上显示

stream - Dart:流中的监听器永远不会触发 onError 函数

firebase - flutter 的 future builder 完成后什么也不显示-Firestore

android - Flutter:不使用 AppBar 时如何在 Android 和 iOS 上更改状态栏文本颜色

php - php中多重继承的最佳方式是什么?

c++ - 如何选择 C++ 中的运算符重载?

css - `vp-preview-invisible` 类被添加到 Safari 中的预览缩略图,但不是在 Chrome 中的 vimeo 嵌入视频