我正在使用 Flutter News 模板创建 Flutter 应用程序,当我尝试检索特定类别的提要时,我不断收到此错误:
_TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast)
。
当我尝试使用应用程序 UI 中的选项卡切换类别时,我确实从端点获取了包含完整 JSON 的日志。我确实需要完整的 JSON,而不是其中的一部分,以便我可以提取应用程序 UI 所需的所有信息。
我添加了一些日志以更好地理解问题。我也尝试在
as Map<String, dynamic>
调用后添加 json()
,但没有帮助。
相关代码:
Future<FeedResponse> getFeed({
Category? category,
int? limit,
int? offset,
}) async {
final uri =
Uri.parse('https://myappurl.com/wp-json/wp/v2/posts?categories=28');
final response = await _httpClient.get(
uri,
// headers: await _getRequestHeaders(),
);
if (response.body is List<dynamic>) {
log('MyLog: Received a list of posts');
} else {
log('MyLog: Unexpected response format:');
}
final body = response.json(); //This line throws the error
if (response.statusCode != HttpStatus.ok) {
throw MyappApiRequestFailure(
body: body,
statusCode: response.statusCode,
);
}
return FeedResponse.fromJson(body);
}
extension on http.Response {
Map<String, dynamic> json() {
try {
final decodedBody = utf8.decode(bodyBytes);
// log('MyLog decoded ${jsonDecode(decodedBody)}');
return jsonDecode(decodedBody) as Map<String, dynamic>; //this line is also marked in the error
} catch (error, stackTrace) {
Error.throwWithStackTrace(
MyappApiMalformedResponse(error: error),
stackTrace,
);
}
}
}
我在调试控制台中遇到的错误:
GetFeedFailure (GetFeedFailure(Instance of 'MyappApiMalformedResponse'))
#0 _extension#0.json (package:myapp_api/src/client/myapp_api_client.dart:370:38)
#1 MyappApiClient.getFeed (package:myapp_api/src/client/myapp_api_client.dart:176:19)
myapp_api_client.dart:176
<asynchronous suspension>
#2 NewsRepository.getFeed (package:news_repository/src/news_repository.dart:76:14)
news_repository.dart:76
<asynchronous suspension>
#3 FeedBloc._updateFeed (package:myapp/feed/bloc/feed_bloc.dart:54:24)
feed_bloc.dart:54
<asynchronous suspension>
#4 Bloc.on.<anonymous closure>.handleEvent (package:bloc/src/bloc.dart:229:13) bloc.dart:229
<asynchronous suspension>
您遇到的错误是因为您收到的响应正文是
List<dynamic>
(帖子数组)而不是 Map<String, dynamic>
。尝试使用此代码:
Future<List<FeedResponse>> getFeed({
Category? category,
int? limit,
int? offset,
}) async {
final uri =
Uri.parse('https://myappurl.com/wp-json/wp/v2/posts?categories=28');
final response = await _httpClient.get(
uri,
// headers: await _getRequestHeaders(),
);
if (response.statusCode != HttpStatus.ok) {
throw MyappApiRequestFailure(
body: response.body,
statusCode: response.statusCode,
);
}
final body = response.json(); // Decoding the response
// Here, handle the case where the response is a list of posts
if (body is List<dynamic>) {
log('MyLog: Received a list of posts');
return body.map((post) => FeedResponse.fromJson(post as Map<String, dynamic>)).toList();
} else {
log('MyLog: Unexpected response format: $body');
throw MyappApiMalformedResponse(error: 'Unexpected response format');
}
}
extension on http.Response {
List<dynamic> json() {
try {
final decodedBody = utf8.decode(bodyBytes);
return jsonDecode(decodedBody) as List<dynamic>; // Decoding as a List<dynamic>
} catch (error, stackTrace) {
Error.throwWithStackTrace(
MyappApiMalformedResponse(error: error),
stackTrace,
);
}
}
}
注意:-希望上面的代码能够正常工作,因为我不确定您在这个 API 中得到了什么。请确保您获取的响应是List。