Reputation: 1335
I am trying to hit an API and then store it into the model? The API response is quite lengthy and consists of multiple level key-value pairs. The API I am hitting is https://gharsansar-backend.herokuapp.com/listing/get/all. This returns a list of some properties from the database. The Model I made for in the Flutter is:
class Listing {
Map property;
Map listedby;
int listPrice;
int featured;
String status;
String id;
String listType;
String listingTitle;
String listingSlug;
String contactNumber;
String specialNote;
String listPriceUnit;
String numOfViewers;
Map viewers;
Map favouriteOf;
Listing();
Listing.fromJSON(Map<String, dynamic> jsonMap) {
print(jsonMap);
try {
property = jsonMap['property'] != null ? jsonMap['property'] : '';
listedby = jsonMap['listedby'] != null ? jsonMap['listedby'] : '';
id = jsonMap['_id'] != null ? jsonMap['_id'] : '';
listPrice = jsonMap['listPrice'] != null ? jsonMap['listPrice'] : '';
listType = jsonMap['listType'] != null ? jsonMap['listType'] : '';
featured = jsonMap['featured'] != null ? jsonMap['featured'] : '';
favouriteOf =
jsonMap['favouriteOf'] != null ? jsonMap['favouriteOf'] : '';
viewers = jsonMap['viewers'] != null ? jsonMap['viewers'] : '';
listingTitle =
jsonMap['listingTitle'] != null ? jsonMap['listingTitle'] : '';
listingSlug =
jsonMap['listingSlug'] != null ? jsonMap['listingSlug'] : '';
status = jsonMap['status'] != null ? jsonMap['status'] : '';
contactNumber =
jsonMap['contactNumber'] != null ? jsonMap['contactNumber'] : '';
specialNote =
jsonMap['specialNote'] != null ? jsonMap['specialNote'] : '';
listPriceUnit =
jsonMap['listPriceUnit'] != null ? jsonMap['listPriceUnit'] : '';
numOfViewers =
jsonMap['numOfViewers'] != null ? jsonMap['numOfViewers'] : '';
} catch (e) {
print(e);
}
}
Map toMap() {
var map = new Map<String, dynamic>();
map["property"] = property;
map["listedby"] = listedby;
map["_id"] = id;
map["listPrice"] = listPrice;
map["listType"] = listType;
map["contactNumber"] = contactNumber;
map["favouriteOf"] = favouriteOf;
map["viewers"] = viewers;
map["listingTitle"] = listingTitle;
map["listingSlug"] = listingSlug;
map["status"] = status;
map["specialNote"] = specialNote;
map["listPriceUnit"] = listPriceUnit;
map["numOfViewers"] = numOfViewers;
return map;
}
@override
String toString() {
var map = this.toMap();
return map.toString();
}
}
I am hitting the API from the flutter using the below code:
Future<Stream<Listing>> getProperties() async {
Uri uri = Helper.getUri('listing/get/all');
print(uri.toString());
Map<String, dynamic> _queryParams = {};
uri = uri.replace(queryParameters: _queryParams);
final client = new http.Client();
final streamedRest = await client.send(http.Request('get', uri));
return streamedRest.stream
.transform(utf8.decoder)
.transform(json.decoder)
.map((data) => Helper.getListings(data))
.expand((data) => (data as List))
.map((data) => Listing.fromJSON(data));
}
I am getting issue probably because of multiple elements inside the property object.
I have attached a screenshot of the logs with the question.
Thanks
Upvotes: 1
Views: 60
Reputation: 12363
Your Map<String,dynamic> is wrapped inside a list.
I copied this:
{"success":true,"data":{"listings":[{"property":{"propertyLocation":{...etc
from the response of the API you posted.
Notice,
"data"
opens up a map which is {"listings":[{"property":{"propertyLocation"...
["listings"]
==> it gives you a LIST
==>
[{"property":{"propertyLocation"...
{"property":{"propertyLocation"...
is the data you want to access, and it's currently sitting at index[0] of this parent list.Try using
should be
map((data) => Helper.getListings(data["listings"]))
to access the ["listings"]
key of the map, which is the list of listings you are looking for.
instead of
map((data) => Helper.getListings(data))
Upvotes: 1