我有一个 Map<String, dynamic> 类型的 JSON 响应,我想从中提取元素到列表中。我想知道如何做到这一点。我已经为它编写了一段代码,但它并没有真正起作用并抛出您将在下面看到的错误。我欢迎各种建议。以下是代码、错误和 JSON 响应:
JSON 响应(我只对获取名为的键(列表)的值感兴趣data
)
{
"status": "200",
"data": {
"current_page": 1,
"data": [ //I need the values in this list
{
"restaurant_id": 1,
"restaurant_name": "City Club",
"restaurant_address": "Street Number 17, GN Block, Sector V, Bidhannagar, Kolkata, West Bengal 700091",
"restaurant_image": "/public/assets/restaurant/6x4aJL03-36-30.jpg",
"restaurant_rating": "4",
"restaurant_rating_count": "8",
"distance": 0
},
{
"restaurant_id": 6,
"restaurant_name": "Mocambo",
"restaurant_address": "Ground Floor, 25B, Mirza Ghalib St, Taltala, Kolkata, West Bengal 700016",
"restaurant_image": "/public/assets/restaurant/A6lAQu03-41-17.jpg",
"restaurant_rating": null,
"restaurant_rating_count": null,
"distance": 14.8039003284490693346242551342584192752838134765625
},
{
"restaurant_id": 7,
"restaurant_name": "Peter Cat",
"restaurant_address": "Park St, opposite KFC Restaurant, Park Street area, Kolkata, West Bengal 700016",
"restaurant_image": "/public/assets/restaurant/RfjxvK03-44-59.jpg",
"restaurant_rating": null,
"restaurant_rating_count": null,
"distance": 47.4211446933120015501117450185120105743408203125
},
{
"restaurant_id": 8,
"restaurant_name": "Flurrys",
"restaurant_address": "Grand Trunk Rd, Barabazar, Sukhsanatantala, Chandannagar, West Bengal 712136",
"restaurant_image": "/public/assets/restaurant/Pitmxq03-47-20.jpg",
"restaurant_rating": null,
"restaurant_rating_count": null,
"distance": 116.161207301201244490584940649569034576416015625
},
{
"restaurant_id": 9,
"restaurant_name": "Karims",
"restaurant_address": "GP Block, Sector V, Bidhannagar, Kolkata, West Bengal 700091",
"restaurant_image": "/public/assets/restaurant/brmWnW03-51-13.jpg",
"restaurant_rating": null,
"restaurant_rating_count": null,
"distance": 179.675331121963466785018681548535823822021484375
}
],
"first_page_url": "https://achievexsolutions.in/current_work/eatiano/api/all_restaurant?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "https://achievexsolutions.in/current_work/eatiano/api/all_restaurant?page=1",
"links": [
{
"url": null,
"label": "« Previous",
"active": false
},
{
"url": "https://achievexsolutions.in/current_work/eatiano/api/all_restaurant?page=1",
"label": "1",
"active": true
},
{
"url": null,
"label": "Next »",
"active": false
}
],
"next_page_url": null,
"path": "https://achievexsolutions.in/current_work/eatiano/api/all_restaurant",
"per_page": 25,
"prev_page_url": null,
"to": 5,
"total": 5
}
}
我写的代码:
class PopularRestaurantProvider with ChangeNotifier {
String baseUrl = 'baseUrl';
Map<String, dynamic> _restaurants = {};
List<dynamic> _restaurantList = [];
final queryParams = {'lat': '22.5735314', 'lng': '88.4331189'};
Map<String, dynamic> get restaurants {
return {..._restaurants};
}
List<dynamic> get restaurantList {
return [..._restaurantList];
}
Future<void> fetchRestaurants() async {
final url = Uri.parse(baseUrl +
'api/all_restaurant' +
'?' +
'lat=${queryParams['lat']}' +
'&' +
'lng=${queryParams['lng']}');
final response = await http.get(url);
PopularRestaurants popularRestaurants =
popularRestaurantsFromJson(response.body);
_restaurants = popularRestaurants.toJson();
_restaurants['data']['data'].forEach((key, value) => _restaurantList.add(value)); //This is where I've tried adding the elements
print('Restaurants List $_restaurantList'); //Currently this doesn't print anything
}
}
我从响应中生成的模型类:
PopularRestaurants popularRestaurantsFromJson(String str) =>
PopularRestaurants.fromJson(json.decode(str));
String popularRestaurantsToJson(PopularRestaurants data) =>
json.encode(data.toJson());
class PopularRestaurants {
PopularRestaurants({
required this.status,
required this.data,
});
String status;
Data data;
factory PopularRestaurants.fromJson(Map<String, dynamic> json) =>
PopularRestaurants(
status: json["status"],
data: Data.fromJson(json["data"]),
);
Map<String, dynamic> toJson() => {
"status": status,
"data": data.toJson(),
};
}
class Data {
Data({
required this.currentPage,
required this.data,
required this.firstPageUrl,
required this.from,
required this.lastPage,
required this.lastPageUrl,
required this.links,
required this.nextPageUrl,
required this.path,
required this.perPage,
required this.prevPageUrl,
required this.to,
required this.total,
});
int currentPage;
List<Datum> data;
String firstPageUrl;
int from;
int lastPage;
String lastPageUrl;
List<Link> links;
dynamic nextPageUrl;
String path;
int perPage;
dynamic prevPageUrl;
int to;
int total;
factory Data.fromJson(Map<String, dynamic> json) => Data(
currentPage: json["current_page"],
data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
firstPageUrl: json["first_page_url"],
from: json["from"],
lastPage: json["last_page"],
lastPageUrl: json["last_page_url"],
links: List<Link>.from(json["links"].map((x) => Link.fromJson(x))),
nextPageUrl: json["next_page_url"],
path: json["path"],
perPage: json["per_page"],
prevPageUrl: json["prev_page_url"],
to: json["to"],
total: json["total"],
);
Map<String, dynamic> toJson() => {
"current_page": currentPage,
"data": List<dynamic>.from(data.map((x) => x.toJson())),
"first_page_url": firstPageUrl,
"from": from,
"last_page": lastPage,
"last_page_url": lastPageUrl,
"links": List<dynamic>.from(links.map((x) => x.toJson())),
"next_page_url": nextPageUrl,
"path": path,
"per_page": perPage,
"prev_page_url": prevPageUrl,
"to": to,
"total": total,
};
}
class Datum {
Datum({
required this.restaurantId,
required this.restaurantName,
required this.restaurantAddress,
required this.restaurantImage,
required this.restaurantRating,
required this.restaurantRatingCount,
required this.distance,
});
int restaurantId;
String restaurantName;
String restaurantAddress;
String restaurantImage;
dynamic restaurantRating;
dynamic restaurantRatingCount;
double distance;
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
restaurantId: json["restaurant_id"],
restaurantName: json["restaurant_name"],
restaurantAddress: json["restaurant_address"],
restaurantImage: json["restaurant_image"],
restaurantRating: json["restaurant_rating"],
restaurantRatingCount: json["restaurant_rating_count"],
distance: json["distance"].toDouble(),
);
Map<String, dynamic> toJson() => {
"restaurant_id": restaurantId,
"restaurant_name": restaurantName,
"restaurant_address": restaurantAddress,
"restaurant_image": restaurantImage,
"restaurant_rating": restaurantRating,
"restaurant_rating_count": restaurantRatingCount,
"distance": distance,
};
}
class Link {
Link({
required this.url,
required this.label,
required this.active,
});
String url;
String label;
bool active;
factory Link.fromJson(Map<String, dynamic> json) => Link(
url: json["url"] == null ? null : json["url"],
label: json["label"],
active: json["active"],
);
Map<String, dynamic> toJson() => {
"url": url == null ? null : url,
"label": label,
"active": active,
};
}
我目前得到的错误:
Unhandled Exception: type '(dynamic) => dynamic' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform
@Belinda 是正确的,因为它_restaurants['data']['data']
是List
.
这就是您的行在这里返回错误的原因,因为它List
本身没有任何keys
or values
。
_restaurants['data']['data'].forEach((key, value) => _restaurantList.add(value)); //This is where I've tried adding the elements
但是,该解决方案将使用每个键的所有单独键的混合填充一个大列表,Map
而没有分离。也许这实际上是您想要的,但我对您的问题的理解是您想要_restaurants['data']['data']
.
如果是这种情况,您可以简单地执行此操作,而不是上面引用的行。
_restaurantList = _restaurants['data']['data'] as List;
List
这将打印此内容,您可以单独访问 中的每个索引Map
。
Restaurants List [{restaurant_id: 1, restaurant_name: City Club, restaurant_address: Street Number 17, GN Block, Sector V, Bidhannagar, Kolkata, West Bengal 700091, restaurant_image: /public/assets/restaurant/6x4aJL03-36-30.jpg, restaurant_rating: 4, restaurant_rating_count: 8, distance: 0.0}, {restaurant_id: 6, restaurant_name: Mocambo, restaurant_address: Ground Floor, 25B, Mirza Ghalib St, Taltala, Kolkata, West Bengal 700016, restaurant_image: /public/assets/restaurant/A6lAQu03-41-17.jpg, restaurant_rating: null, restaurant_rating_count: null, distance: 14.80390032844907}, {restaurant_id: 7, restaurant_name: Peter Cat, restaurant_address: Park St, opposite KFC Restaurant, Park Street area, Kolkata, West Bengal 700016, restaurant_image: /public/assets/restaurant/RfjxvK03-44-59.jpg, restaurant_rating: null, restaurant_rating_count: null, distance: 47.421144693312}, {restaurant_id: 8, restaurant_name: Flurrys, restaurant_address: Grand Trunk Rd, Barabazar, Sukhsanatantala, Chandannaga<…>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句