json - 未处理的异常:类型 '_InternalLinkedHashMap' 不是类型 'String' 的子类型
问题描述
我试图解析 json,将其打印到控制台,然后放入 ListView.builder 并收到此错误:类型 '_InternalLinkedHashMap<String, dynamic>' 不是类型 '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");
}
}
模型
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);
推荐阅读
- angular - 添加自定义属性以增强联合登录
- javascript - 模拟 axios 接收空对象
- amazon-web-services - AWS CDK -- 错误:在 Azure DevOps 管道中找不到模块“@aws-cdk/cloud-assembly-schema”
- asp.net - 您如何使用没有明显扩展名的网站 URL
- flutter - 在 Flutter 中缩小文本基线以下的区域
- g++ - 如何将 rpath 与 LLD 链接器 (LLVM) 一起使用
- react-hooks - React 状态变量被覆盖(useState)
- regex - 表示二进制 32 位十进制数的最佳正则表达式是什么?
- tailwind-css - 如何使用 watch 编译设置 tailwindcss
- mysql - MySQL 通过 id 连接 2 个表 A,B,其中 A.id 是 B.id 的子字符串