Flutter: getting an 'Expected a value of type Iterable<dynamic>, but got one of type `_JsonMap`' error when retrieving JSON data from a URL

huangapple go评论100阅读模式
英文:

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',但得到的类型是'_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),
                );
              }),
        ));
  }
}

这里是堆栈跟踪:
错误: 期望的类型是'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 &#39;dart:convert&#39;;
import &#39;package:flutter/material.dart&#39;;

import &#39;../data/character_api.dart&#39;;
import &#39;../model/character.dart&#39;;

class CharacterList extends StatefulWidget {
  const CharacterList({Key? key}) : super(key: key);

  @override
  _CharacterListState createState() =&gt; _CharacterListState();
}

class _CharacterListState extends State&lt;CharacterList&gt; {
  List&lt;Character&gt; characterList = &lt;Character&gt;[];

  void getCharactersfromApi() async {
    CharacterApi.getCharacters().then((response) {
      setState(() {
        Iterable list = json.decode(response.body);
        characterList = list.map((model) =&gt; Character.fromJson(model)).toList();
      });
    });
  }

  @override
  void initState() {
    super.initState();
    getCharactersfromApi();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(&quot;Breaking Bad Characters&quot;),
        ),
        body: Container(
          child: ListView.builder(
              itemCount: characterList.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(&quot;${characterList[index].id}&quot;),
                  subtitle: Text(characterList[index].title),
                );
              }),
        ));
  }
}

here is stacktrace
Error: Expected a value of type &#39;Iterable&lt;dynamic&gt;&#39;, but got one of type &#39;_JsonMap&#39;
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 &lt;fn&gt;
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 &lt;fn&gt;
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 &lt;fn&gt;

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;

huangapple
  • 本文由 发表于 2023年6月5日 13:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定