【问题标题】:Flutter Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic> JSON requestFlutter 未处理的异常:类型 '_InternalLinkedHashMap<String, dynamic> JSON 请求
【发布时间】:2020-02-17 16:26:17
【问题描述】:

我是 Flutter 的初学者,当我想从 JSON 中获取值时,我的应用程序出现问题。

当我调用GetCommands() 时,问题来自我的http 请求,我不知道如何解决它。 我已经在另一个应用程序中用其他数据(JSON 也是)做过一次,它工作了......

这里是主要代码:

  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
    });
  }

这是我的价值类:

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;
  }
}

这里是调试控制台错误:

[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<…>

这是 JSON 数据:

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

非常感谢您的帮助。

【问题讨论】:

    标签: json exception flutter get request


    【解决方案1】:

    我认为问题是response.body,是什么类型的?如果 response.body 已经是 String 的 map 并且是动态的,则不需要使用 json.decode,只需 Value.fromJson(model)

    尝试修改、删除解码和解析直接response.body,如下所示:

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

    如果不行,请传递你使用什么包进行http调用。

    我已经用这个 url http://www.mocky.io/v2/5e4b0ef82f000000bc97d6ee 和 Dio 包进行了测试。

    【讨论】:

      猜你喜欢
      • 2021-08-01
      • 2021-03-12
      • 2021-07-11
      • 2021-01-17
      • 1970-01-01
      • 2021-09-12
      • 2020-08-20
      • 1970-01-01
      • 2021-09-13
      相关资源
      最近更新 更多