Trying to get plant info and found https://trefle.io/ Api, to use in my flutter app, i need list of plant infos, so i found a json structure like this;
I'm trying fetch data from api array and implement it my ListView.builder, but i'm getting error;
type 'Future' is not a subtype of type 'Map<String, dynamic>'
So What's the most efficient way of fetching array data from json
List View
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),
),
);
}),
FETCH DATA
Future<dynamic> fetchData(String url, bool asyncCall) async {
asyncCall = true;
response = await http.get(url);
decoded = json.encode(response.body);
return decoded;
}
PLANT
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;
}
}
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;
}
}
To Solve
Added this func below
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 {}
}
changed 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;
}
