首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >_InternalLinkedHashMap<dynamic,dynamic>‘不是'Map<String,dynamic>’类型的子类型

_InternalLinkedHashMap<dynamic,dynamic>‘不是'Map<String,dynamic>’类型的子类型
EN

Stack Overflow用户
提问于 2022-11-24 10:18:30
回答 5查看 119关注 0票数 0

我有一个错误,这是我的API包装器

代码语言:javascript
复制
    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');
      }
  . . .

这是存储库:

代码语言:javascript
复制
  Future<LeaveListModel> fetchResponse(query) async {
    final response = await _wrapper.apiRequest(
        "get", _wrapper.leaveListGetData, {'params': query}, true);
    return LeaveListModel.fromJson(response);
  }

如果您想知道的话,这就是服务:

代码语言:javascript
复制
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文件,但是它显示了一个错误,我不知道我的代码有什么问题,如果您知道如何修复它,请告诉我

这是一个错误:

代码语言:javascript
复制
  I/flutter (20696): Call https:xxxxxxxxxxxx
   I/flutter (20696): type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'

另一个错误说明:

代码语言:javascript
复制
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包装器,只是为了显示进程在哪里停止并得到错误。

EN

回答 5

Stack Overflow用户

发布于 2022-11-24 11:14:31

您需要decode您的响应,然后使用它:

代码语言:javascript
复制
LeaveListModel.fromJson(jsonDecode(response));

此外,我认为您正在将编码的query传递给ApiWrapper,因此您需要像这样对其进行解码:

代码语言:javascript
复制
Map<String, dynamic> params = data['params'] != null ? jsonDecode(data['params']) : {};
票数 0
EN

Stack Overflow用户

发布于 2022-11-28 06:33:00

代码语言:javascript
复制
return LeaveListModel.fromJson(response);

在我看来,您应该在这里使用response.data,而不是响应。

代码语言:javascript
复制
return LeaveListModel.fromJson(response.data);
票数 0
EN

Stack Overflow用户

发布于 2022-11-28 07:25:07

试试这个:

代码语言:javascript
复制
Map<String, dynamic>.from(yourData)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74559078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档