There's a URL address that I decoded to JSON, and there's a certain header (result) that I'm trying to convert to a list of objects (Book).
When I print the content of this header I do get the desired result (first print), but when I create the list and try to print one of the fields of the first object in the list nothing is printed (second print).
Which means there's a problem with the way I create the list and I can't find it.
I would really appreciate your help.
The JSON structure:
{count: 9447, result: [{book_about: string, book_label: string, book_wikilink: string, fragments: [string1, string2, string3, string4], id: string, label: string, text: string]}
An image of it: https://i.sstatic.net/w2JGm.png
My code:
Future<List<Book>> fetchBooks(http.Client client) async {
var url = 'http://jbse.cs.technion.ac.il:3030/search?query=אברהם&start=0';
final response = await http.get(Uri.parse(url),headers: {'Content-Type': 'application/json'});
Map<String, dynamic> responseBody = jsonDecode(response.body);
dynamic results = responseBody['result'];
print(results);
List<Book> list = results.map((book) => Book.fromJson(book)).toList();
print(list.first.book_wikilink);
return list;
}
class Book {
String book_about;
String book_label;
String book_wikilink;
List<String> fragments;
String id;
String label;
String text;
Book(
this.book_about,
this.book_label,
this.book_wikilink,
this.fragments,
this.id,
this.label,
this.text
);
factory Book.fromJson(Map<String, dynamic> json) {
return Book(
json['book_about'],
json['book_label'],
json['book_wikilink'] ,
json['fragments'],
json['id'],
json['label'],
json['text'],
);
}
}