英文:
Flutter: getting an 'Expected a value of type Iterable<dynamic>, but got one of type `_JsonMap`' error when retrieving JSON data from a URL
问题
错误: 期望的类型是'Iterable
import 'dart:convert';
import 'package:flutter/material.dart';
import '../data/character_api.dart';
import '../model/character.dart';
class CharacterList extends StatefulWidget {
const CharacterList({Key? key}) : super(key: key);
@override
_CharacterListState createState() => _CharacterListState();
}
class _CharacterListState extends State<CharacterList> {
List<Character> characterList = <Character>[];
void getCharactersfromApi() async {
CharacterApi.getCharacters().then((response) {
setState(() {
Iterable list = json.decode(response.body);
characterList = list.map((model) => Character.fromJson(model)).toList();
});
});
}
@override
void initState() {
super.initState();
getCharactersfromApi();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Breaking Bad Characters"),
),
body: Container(
child: ListView.builder(
itemCount: characterList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text("${characterList[index].id}"),
subtitle: Text(characterList[index].title),
);
}),
));
}
}
这里是堆栈跟踪:
错误: 期望的类型是'Iterable<dynamic>',但得到的类型是'_JsonMap'
。我试图从URL获取简单的JSON以测试Flutter和JSON集成,只是想在Flutter中将JSON数据显示为UI文本。
英文:
Error: Expected a value of type 'Iterable<dynamic>', but got one of type '_JsonMap' Flutter
import 'dart:convert';
import 'package:flutter/material.dart';
import '../data/character_api.dart';
import '../model/character.dart';
class CharacterList extends StatefulWidget {
const CharacterList({Key? key}) : super(key: key);
@override
_CharacterListState createState() => _CharacterListState();
}
class _CharacterListState extends State<CharacterList> {
List<Character> characterList = <Character>[];
void getCharactersfromApi() async {
CharacterApi.getCharacters().then((response) {
setState(() {
Iterable list = json.decode(response.body);
characterList = list.map((model) => Character.fromJson(model)).toList();
});
});
}
@override
void initState() {
super.initState();
getCharactersfromApi();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Breaking Bad Characters"),
),
body: Container(
child: ListView.builder(
itemCount: characterList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text("${characterList[index].id}"),
subtitle: Text(characterList[index].title),
);
}),
));
}
}
here is stacktrace
Error: Expected a value of type 'Iterable<dynamic>', but got one of type '_JsonMap'
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:49 throw_
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 99:3 castError
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 485:10 cast
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/classes.dart 638:14 as_C
packages/rexello_tests_only/full_custom_test/flutter_json_api/screens/character_list.dart 20:18 <fn>
packages/flutter/src/widgets/framework.dart 1133:30 setState
packages/rexello_tests_only/full_custom_test/flutter_json_api/screens/character_list.dart 19:7 <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1660:54 runUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 147:18 handleValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 767:44 handleValueCallback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 796:13 _propagateToListeners
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 567:5 [_completeWithValue]
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 640:7 callback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask.dart 40:11 _microtaskLoop
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask.dart 49:5 _startMicrotaskLoop
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 166:15 <fn>
I was trying to get a simple json from url to test flutter and json integration . just wanted to show json data as ui text in flutter .
答案1
得分: 1
- 在你的代码中的这一行:你正在将响应解码为一个映射,而且你将返回类型设置为一个列表。只需将 Iterable list 替换为 var response。
var response = json.decode(response.body);
或者你可以这样写:
- 用于将响应解码为列表的代码:
List list = (json.decode(response.body)) as List;
英文:
- Iterable list = json.decode(response.body);
in this line of your code: you're decoding a response to a map, and you put the return type is a list. just replace Iterable list to var response.
var response = json.decode(response.body);
or you can make this
- Listlist = (json.decode(response.body)) as List;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论