简体   繁体   English

Flutter 未处理的异常:类型 &#39;_InternalLinkedHashMap<String, dynamic> JSON 请求

[英]Flutter Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic> JSON request

I'm a beginner in flutter and i have problem with my app when i wanna take the values from the JSON.我是 flutter 的初学者,当我想从 JSON 中获取值时,我的应用程序有问题。

The problem comes from my http request when i call GetCommands() and i don't know to solve it.问题来自我调用GetCommands()时的 http 请求,但我不知道如何解决它。 I already did it once in another app with other data(JSON too) and it worked...我已经在另一个应用程序中使用其他数据(也使用 JSON)做过一次,并且它有效......

Here is the main code :这是主要代码:

  var commandsGet = new List<Value>();

  Future<void> GetCommands() async {
    await GET.url(urlTest).then((response) {
      setState(() {
        Iterable list2 = json.decode(response.body);
        commandsGet = list2.map((model) => Value.fromJson(model)).toList();
      });
    });
  }

  Future<void> wait2() async {
    await GetCommands();
  }

  onChange2(commandsData user) {
    setState(() {
      commandselection = user;
      // TEST 
      wait2().then((result) {
        print("This is a test");
      });
      // TEST
    });
  }

Here is my Value class :这是我的价值类:

class Value {
  String device;
  int origin;
  List<Readings> readings;

  Value({
    this.device,
    this.origin,
    this.readings,
  });

  Value.fromJson(Map<String, dynamic> json) {
    device = json['device'];
    origin = json['origin'];
    if (json['readings'] != null) {
      readings = new List<Readings>();
      json['readings'].forEach((v) {
        readings.add(new Readings.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data2 = new Map<String, dynamic>();
    data2['device'] = this.device;
    data2['origin'] = this.origin;
    if (this.readings != null) {
      data2['readings'] = this.readings.map((v) => v.toJson()).toList();
    }
    return data2;
  }
}

And here is the the Debug console errors :这是调试控制台错误:

[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>'
#0      _MyListScreenState.GetCommands.<anonymous closure>.<anonymous closure> 
package:chat/main.dart:92
#1      State.setState 
package:flutter/…/widgets/framework.dart:1164
#2      _MyListScreenState.GetCommands.<anonymous closure> 
#3      _rootRunUnary  (dart:async/zone.dart:1134:38)
#4      _CustomZone.runUnary  (dart:async/zone.dart:1031:19)
#5      _FutureListener.handleValue  (dart:async/future_impl.dart:140:18)
#6      Future._propagateToListeners.handleValueCallback  (dart:async/future_impl.dart:682:45)
#7      Future._propagateToListeners  (dart:async/future_impl.dart:711:32)
#8      Future._completeWithValue  (dart:async/future_impl.dart:526:5)
#9      _AsyncAwaitCompleter.complete  (dart:async-patch/async_patch.dart:34:15)
#10     _completeOnAsyncReturn  (dart:async-patch/async_patch.dart:293:13)
#11     _withCl<…>

Here is the JSON data :这是 JSON 数据:

{
    "device": "Random-Integer-Generator01",
    "origin": 1581956092184,
    "readings": [
        {
            "origin": 1581956092183,
            "device": "Random-Integer-Generator01",
            "name": "RandomValue_Int8",
            "value": "23"
        }
    ]
}

Thanks a lot for your help.非常感谢你的帮助。

I think the problem is response.body , what is the type?我认为问题是response.body ,类型是什么? If the response.body is map of String and dynamic already, you dont need to use json.decode , just Value.fromJson(model)如果 response.body 已经是 String 和 dynamic 的映射,则不需要使用json.decode ,只需使用Value.fromJson(model)

Try to modify, removing decode and parsing direct response.body to list like this:尝试修改、删除解码和解析直接 response.body 以列出如下:

(response.body as List).map((item) => Value.fromJson(item)).toList()

If not work, please, pass what package do you use to make http call.如果不起作用,请传递您使用什么包进行http调用。

I've tested with this url http://www.mocky.io/v2/5e4b0ef82f000000bc97d6ee and Dio package.我已经用这个 url http://www.mocky.io/v2/5e4b0ef82f000000bc97d6ee和 Dio 包进行了测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter dart json 未处理的异常:InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic></dynamic></string,> - Flutter dart json Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic> flutter json 未处理的异常:类型 '_InternalLinkedHashMap - flutter json Unhandled Exception: type '_InternalLinkedHashMap 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic></string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic> E/Flutter 未处理异常:类型 _InternalLinkedHashMap<dynamic, dynamic> ' 不是类型 'Map 的子类型<string, string> ?</string,></dynamic,> - E/Flutter Unhandled Exception: type _InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, String>? 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型转换 int flutter 中类型 'OriginDestination' 的子类型</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'OriginDestination' in type cast int flutter Dio.get - 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是 Flutter/Dart 中 'String' 类型的子类型</string,> - Dio.get - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' in Flutter/Dart 从 api 错误中获取 Json 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic> '</dynamic></string,> - Fetching Json from api error Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' 错误:未处理的异常:类型 &#39;_InternalLinkedHashMap<String, dynamic> &#39; 不是 Flutter 中类型 &#39;int&#39; 的子类型 - Error: Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'int' in Flutter 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'String' 的子类型</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' 未处理的异常:InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic></dynamic></string,> - Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM