how to read getter variable when it is returned from factory method? I have a class names Book like this
class Book {
final String _name;
String get name => _name;
Program(this._name);
factory Program.fromJson(List<dynamic> json) {
return Program(
json[0]['book_name'],
);
}
}
} and I'm fetchings book from api like this
Future<Book> fetchBook() async {
final response =
await http.get('http://book.com');
if (response.statusCode == 200) {
return Book.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load book');
}
}
and I'm calling this method like this
book = fetchBook();
print(book.name)
but getting this error
The getter 'name' isn't defined for the type 'Future<Book>'.
Try importing the library that defines 'name', correcting the name to the name of an existing getter, or defining a getter or field named 'name'.
How get name of the book?