我有一个错误,这是我的API包装器
if (method == "get") {
var param = '';
Map<String, dynamic> params =
data['params'] != null ? data['params'] : {};
params.forEach((k, v) => param += k + "=" + (v == null ? '' : v) + "&");
try {
Dio dio = new Dio();
dio.options.headers = headers;
final response = await dio.get(url + "?" + param);
responseJson = _response(response);
//print('Response ' + response.data.toString());
} on SocketException {
throw FetchDataException('Tidak terhubung ke server');
}
. . .这是存储库:
Future<LeaveListModel> fetchResponse(query) async {
final response = await _wrapper.apiRequest(
"get", _wrapper.leaveListGetData, {'params': query}, true);
return LeaveListModel.fromJson(response);
}如果您想知道的话,这就是服务:
class Service {
GetStorage localData = GetStorage();
final String initial = "Service";
final String baseUrl = ConstantConfig().leaveListEndPoint;
final String appCode = ConstantConfig().leaveListAppCode;
final String outputType = "json";
final String routeAuthConnect = "auth/connect/";
final String routeAuthGetAccessToken = "auth/getAccessToken/";
final String leaveListGetData = "list/";
Future<dynamic> apiRequest(
String method, String route, Map<String, dynamic> data,
[bool needToken = false]) async {
ApiWrapper _apiWrapper = ApiWrapper();
if (needToken) {
int thisTime = (DateTime.now().millisecondsSinceEpoch / 1000).round();
String? savedExpired = localData.read(initial + KeyStorage.accessExpired);
int expire = savedExpired == null ? 0 : int.parse(savedExpired);
if (expire < thisTime) {
dynamic tokenResponse = await _apiWrapper.request(baseUrl, initial,
appCode, outputType, 'post', routeAuthGetAccessToken, {});
await localData.write(initial + KeyStorage.accessToken,
tokenResponse['response']['access_token']);
return await _apiWrapper.request(
baseUrl, initial, appCode, outputType, method, route, data);
} else {
return await _apiWrapper.request(
baseUrl, initial, appCode, outputType, method, route, data);
}
} else {
return await _apiWrapper.request(
baseUrl, initial, appCode, outputType, method, route, data);
}
}
}我只想调用一个json文件,但是它显示了一个错误,我不知道我的代码有什么问题,如果您知道如何修复它,请告诉我
这是一个错误:
I/flutter (20696): Call https:xxxxxxxxxxxx
I/flutter (20696): type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'另一个错误说明:
I/flutter ( 7626): {}
I/flutter ( 7626): 1
I/flutter ( 7626): type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'
I/flutter ( 7626): {}
I/flutter ( 7626): 1
I/flutter ( 7626): type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'实际上,这表明进程在Map<String, dynamic> params = data['params'] != null ? data['params'] : {};上停止了,因为我尝试用print('1');、print('2');、print('3');调试API包装器,只是为了显示进程在哪里停止并得到错误。
发布于 2022-11-24 11:14:31
您需要decode您的响应,然后使用它:
LeaveListModel.fromJson(jsonDecode(response));此外,我认为您正在将编码的query传递给ApiWrapper,因此您需要像这样对其进行解码:
Map<String, dynamic> params = data['params'] != null ? jsonDecode(data['params']) : {};发布于 2022-11-28 06:33:00
return LeaveListModel.fromJson(response);在我看来,您应该在这里使用response.data,而不是响应。
return LeaveListModel.fromJson(response.data);发布于 2022-11-28 07:25:07
试试这个:
Map<String, dynamic>.from(yourData)https://stackoverflow.com/questions/74559078
复制相似问题