颤振未处理异常:类型'_内部链接dhashmap<;字符串,动态>;JSON请求
我是Flatter的初学者,当我想从JSON获取值时,我的应用程序有问题 这个问题来自我调用颤振未处理异常:类型'_内部链接dhashmap<;字符串,动态>;JSON请求,json,exception,flutter,get,request,Json,Exception,Flutter,Get,Request,我是Flatter的初学者,当我想从JSON获取值时,我的应用程序有问题 这个问题来自我调用GetCommands()时的http请求,我不知道如何解决它。 我已经在另一个应用程序中用其他数据(JSON)做过一次了,而且效果很好 以下是主要代码: var commandsGet = new List<Value>(); Future<void> GetCommands() async { await GET.url(urlTest).then((resp
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
});
}
非常感谢你的帮助 我认为问题在于
响应。body
,是什么类型的?如果response.body已经是字符串和动态的映射,则不需要使用json.decode
,只需Value.fromJson(model)
尝试修改、删除解码并解析direct response.body,如下所示:
(response.body as List).map((item) => Value.fromJson(item)).toList()
如果不工作,请传递您用来进行http调用的包
我已经用这个url和Dio包进行了测试
[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<…>
{
"device": "Random-Integer-Generator01",
"origin": 1581956092184,
"readings": [
{
"origin": 1581956092183,
"device": "Random-Integer-Generator01",
"name": "RandomValue_Int8",
"value": "23"
}
]
}
(response.body as List).map((item) => Value.fromJson(item)).toList()