[英]Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>
[英]Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
我正在嘗試解析 json,將其打印到控制台,然后放入 ListView.builder 並收到此錯誤:type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
我如何解決它?
列表數據
FutureBuilder(
future: restaurantSearch,
builder: (context, AsyncSnapshot<RestaurantSearch> snapshot) {
if(snapshot.connectionState == ConnectionState.waiting) {
return Expanded(child: Center(child: CircularProgressIndicator(strokeWidth: 3)));
} else if(snapshot.connectionState == ConnectionState.done) {
if(snapshot.hasData) {
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data.restaurants.length,
itemBuilder: (context, index) {
var restaurant = snapshot.data.restaurants[index];
return ListTile(
title:Text(restaurant.name),
);
}
);
}
} else if (snapshot.hasError) {
return Center(child: Text(snapshot.error.toString()));
}
return Text('');
}
)
api服務
Future<RestaurantSearch> restaurantSearch(String query) async {
final response = await dio.get("https://restaurant-api.dicoding.dev/search", queryParameters: {"q": query});
print(response.data);
if(response.statusCode == 200) {
return RestaurantSearch.fromJson(json.decode(response.data));
} else {
throw Exception("Failed to Load Detail Restaurant, Please Check Your Internet");
}
}
model
import 'dart:convert';
RestaurantSearch restaurantSearchFromJson(String str) => RestaurantSearch.fromJson(json.decode(str));
String restaurantSearchToJson(RestaurantSearch data) => json.encode(data.toJson());
class RestaurantSearch {
RestaurantSearch({
this.error,
this.founded,
this.restaurants,
});
bool error;
int founded;
List<RestaurantS> restaurants;
factory RestaurantSearch.fromJson(Map<String, dynamic> json) => RestaurantSearch(
error: json["error"],
founded: json["founded"],
restaurants: List<RestaurantS>.from(json["restaurants"].map((x) => RestaurantS.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"error": error,
"founded": founded,
"restaurants": List<String>.from(restaurants.map((x) => x.toJson())),
};
}
class RestaurantS {
RestaurantS({
this.id,
this.name,
this.description,
this.pictureId,
this.city,
this.rating,
});
String id;
String name;
String description;
String pictureId;
String city;
double rating;
factory RestaurantS.fromJson(Map<String, dynamic> json) => RestaurantS(
id: json["id"],
name: json["name"],
description: json["description"],
pictureId: json["pictureId"],
city: json["city"],
rating: json["rating"].toDouble(),
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"description": description,
"pictureId": pictureId,
"city": city,
"rating": rating,
};
}
非常感謝您的回答........................................... ..................................................... ..................................................... ..................................................... ...................................................
json.decode
因為 Dio 已經為您解碼身體更改此行
return RestaurantSearch.fromJson(response.data);
Dio已經解碼了身體所以不需要解碼身體
RestaurantSearch.fromJson(response.data);
of if use print("值為"+response.data);
刪除數據,所以它會喜歡
print("值為"+response.toString);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.