arrays - 从 json flutter 获取数组

标签 arrays json flutter fetch

试图获取植物信息并发现https://trefle.io/ Api,要在我的 flutter 应用程序中使用,我需要植物信息列表,所以我找到了这样的 json 结构;

enter image description here

我正在尝试从 api 数组获取数据并在 ListView.builder 中实现它,但出现错误;

类型“Future”不是类型“Map”的子类型

那么从 json 获取数组数据最有效的方法是什么

ListView

ListView.builder(
                          scrollDirection: Axis.horizontal,
                          itemCount: 10,
                          itemBuilder: (BuildContext context, int index) {
                            Plant plant = Plant.fromJson(decodedData);
                            Data data = plant.data[index];
                            Container(
                              child: Image(
                                image: NetworkImage(data.imageUrl),
                              ),
                            );
                          }),

获取数据

  Future<dynamic> fetchData(String url, bool asyncCall) async {
    asyncCall = true;
    response = await http.get(url);
    decoded = json.encode(response.body);

    return decoded;
  }

植物

class Plant {
  List<Data> data;

  Plant({this.data});

  Plant.fromJson(Map<String, dynamic> json) {
    if (json['data'] != null) {
      data = new List<Data>();
      json['data'].forEach((v) {
        data.add(new Data.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.data != null) {
      data['data'] = this.data.map((v) => v.toJson()).toList();
    }

    return data;
  }
}

数据

class Data {
  int id;
  String commonName;
  String slug;
  String scientificName;
  int year;
  String bibliography;
  String author;
  String status;
  String rank;
  String familyCommonName;
  int genusId;
  String imageUrl;
  List<String> synonyms;
  String genus;
  String family;

  Data({
    this.id,
    this.commonName,
    this.slug,
    this.scientificName,
    this.year,
    this.bibliography,
    this.author,
    this.status,
    this.rank,
    this.familyCommonName,
    this.genusId,
    this.imageUrl,
    this.synonyms,
    this.genus,
    this.family,
  });

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    commonName = json['common_name'];
    slug = json['slug'];
    scientificName = json['scientific_name'];
    year = json['year'];
    bibliography = json['bibliography'];
    author = json['author'];
    status = json['status'];
    rank = json['rank'];
    familyCommonName = json['family_common_name'];
    genusId = json['genus_id'];
    imageUrl = json['image_url'];
    synonyms = json['synonyms'].cast<String>();
    genus = json['genus'];
    family = json['family'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['common_name'] = this.commonName;
    data['slug'] = this.slug;
    data['scientific_name'] = this.scientificName;
    data['year'] = this.year;
    data['bibliography'] = this.bibliography;
    data['author'] = this.author;
    data['status'] = this.status;
    data['rank'] = this.rank;
    data['family_common_name'] = this.familyCommonName;
    data['genus_id'] = this.genusId;
    data['image_url'] = this.imageUrl;
    data['synonyms'] = this.synonyms;
    data['genus'] = this.genus;
    data['family'] = this.family;

    return data;
  }
}

解决

在下面添加了这个功能

 Plant plant;
  Future<void> getPlant() async {
    String url =
        'https://trefle.io/api/v1/plants?token=jAEYseuuPFUlUss9QcNOefanIBG_fb83mkXdaRDIu8w';
    Map<String, String> header = {"Content-type": "application/json"};
    // make GET request
    var response = await http.get(url, headers: header);
    // check the status code for the result
    if (response.statusCode == 200) {
      setState(() {
        plant = plantFromJson(response.body);
      });
    } else {}
  }

更改了 fetchData

 Future<Plant> fetchData(String url, bool asyncCall) async {
    asyncCall = true;
    response = await http.get(url);

    final plant = plantFromJson(response.body);
    print(plant);
    print(plant.data);
    print(plant.data[0].imageUrl);
    return plant;
  }

最佳答案

    import 'dart:convert';

Plant plantFromJson(String str) => Plant.fromJson(json.decode(str));

String plantToJson(Plant data) => json.encode(data.toJson());

class Plant {
  Plant({
    this.data,
    this.links,
    this.meta,
  });

  List<Data> data;
  PlantLinks links;
  Meta meta;

  factory Plant.fromJson(Map<String, dynamic> json) => Plant(
    data: List<Data>.from(json["data"].map((x) => Data.fromJson(x))),
    links: PlantLinks.fromJson(json["links"]),
    meta: Meta.fromJson(json["meta"]),
  );

  Map<String, dynamic> toJson() => {
    "data": List<dynamic>.from(data.map((x) => x.toJson())),
    "links": links.toJson(),
    "meta": meta.toJson(),
  };
}

class Data {
  Data({
    this.author,
    this.bibliography,
    this.commonName,
    this.family,
    this.familyCommonName,
    this.genus,
    this.genusId,
    this.id,
    this.links,
    this.plantId,
    this.rank,
    this.scientificName,
    this.slug,
    this.status,
    this.synonyms,
    this.year,
  });

  String author;
  String bibliography;
  dynamic commonName;
  String family;
  String familyCommonName;
  String genus;
  int genusId;
  int id;
  DatumLinks links;
  int plantId;
  String rank;
  String scientificName;
  String slug;
  String status;
  List<String> synonyms;
  int year;

  factory Data.fromJson(Map<String, dynamic> json) => Data(
    author: json["author"],
    bibliography: json["bibliography"],
    commonName: json["common_name"],
    family: json["family"],
    familyCommonName: json["family_common_name"] == null ? null : json["family_common_name"],
    genus: json["genus"],
    genusId: json["genus_id"],
    id: json["id"],
    links: DatumLinks.fromJson(json["links"]),
    plantId: json["plant_id"],
    rank: json["rank"],
    scientificName: json["scientific_name"],
    slug: json["slug"],
    status: json["status"],
    synonyms: List<String>.from(json["synonyms"].map((x) => x)),
    year: json["year"],
  );

  Map<String, dynamic> toJson() => {
    "author": author,
    "bibliography": bibliography,
    "common_name": commonName,
    "family": family,
    "family_common_name": familyCommonName == null ? null : familyCommonName,
    "genus": genus,
    "genus_id": genusId,
    "id": id,
    "links": links.toJson(),
    "plant_id": plantId,
    "rank": rank,
    "scientific_name": scientificName,
    "slug": slug,
    "status": status,
    "synonyms": List<dynamic>.from(synonyms.map((x) => x)),
    "year": year,
  };
}

class DatumLinks {
  DatumLinks({
    this.genus,
    this.plant,
    this.self,
  });

  String genus;
  String plant;
  String self;

  factory DatumLinks.fromJson(Map<String, dynamic> json) => DatumLinks(
    genus: json["genus"],
    plant: json["plant"],
    self: json["self"],
  );

  Map<String, dynamic> toJson() => {
    "genus": genus,
    "plant": plant,
    "self": self,
  };
}

class PlantLinks {
  PlantLinks({
    this.first,
    this.last,
    this.next,
    this.self,
  });

  String first;
  String last;
  String next;
  String self;

  factory PlantLinks.fromJson(Map<String, dynamic> json) => PlantLinks(
    first: json["first"],
    last: json["last"],
    next: json["next"],
    self: json["self"],
  );

  Map<String, dynamic> toJson() => {
    "first": first,
    "last": last,
    "next": next,
    "self": self,
  };
}

class Meta {
  Meta({
    this.total,
  });

  int total;

  factory Meta.fromJson(Map<String, dynamic> json) => Meta(
    total: json["total"],
  );

  Map<String, dynamic> toJson() => {
    "total": total,
  };
}

响应=等待http.get(url); 最终 plant = plantFromJson(response.body)//获取解析数据

关于arrays - 从 json flutter 获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65230954/

相关文章:

firebase - 查询 Firestore 以获取一系列 documentID 中的文档

Java - 存储单个值和一条相应信息的最佳方式?

arrays - 更新 NSManagedObjects 数组

java - 如何根据特定键的值 null 删除 JSON 数组中的 JSON 对象?

json - facebook graph api 的 Swagger 文档

用于将 .ini 文件转换为 .json 文件的 Javascript 库(客户端)

Flutter:运行 "flutter pub get"没有任何反应

dart - Flutter 错误 - 内核二进制格式版本无效(找到 4 个,预期为 6 个)

javascript - 将嵌套集合属性拉入数组

c# - 找到两个数组之间差异的更好方法