Flutter:从URL检索JSON数据时,出现“Expected a value of type Iterable< dynamic>,but got one of type `_JsonMap`”错误

vltsax25  于 2023-06-07  发布在  Flutter
关注(0)|答案(1)|浏览(365)

错误:需要类型为'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),
                );
              }),
        ));
  }
}

下面是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>
我试着从url得到一个简单的json来测试flutter和json的集成。只是想在flutter中将JSON数据显示为UI文本。

ycl3bljg

ycl3bljg1#

  • Iterable list = json.decode(response.body);

在代码的这一行中:你正在解码一个Map的响应,你把返回类型是一个列表。只需将***Iterable list***替换为***var response***即可。var response = json.decode(response.body);或者你可以让这个

  • Listlist =(json.decode(response.body))as List;

相关问题