当前位置: 首页 > 知识库问答 >
问题:

Dart未处理异常:类型“\u InternalLinkedHashMap”不是类型“Iterable的子类型

商开宇
2023-03-14

如何修复未经处理的异常问题:键入“\u InternalLinkedHashMap”

//Sample Json//
{
  "data": [
    {
      "title": "Ronix",
      "year": "Year 5",
      "category": "Admin"
    },
    {
      "title": "Ultra",
      "year": "Year 11",
      "category": "Tutor"
    },
    {
      "title": "Sonet",
      "year": "Year 3",
      "category": "Admin"
    }
  ]
}

这是我的班级。省道锉

class DataClass{
  String title ="";
  String year="";
  String category="";

  DataClass(this.title, this.year, this.category);

  DataClass.fromJson(Map<String, dynamic> json){
    title = json["title"];
    year = json["year"];
    category = json["category"];
  }
}

还有我的主菜。省道锉

class CreateState1 extends State<JsonData1>{
  List<DataClass> _dataClass = [];

  Future<List<DataClass>> getMovieData() async {
    var response = await http.get(Uri.parse("myjsonApiUrl"));
    var datas = [];
    if(response.statusCode == 200){
      var datasJson = json.decode(response.body);
      for(var dataJson in datasJson){
        datas.add(DataClass.fromJson(dataJson));
      }
    }
    return _dataClass;
  }
  @override
  Widget build(BuildContext context) {
    getMovieData().then((value) {
      setState(() {
        _dataClass.addAll(value);
      });
    });

    return Scaffold(
      backgroundColor: Colors.cyan,
      body: ListView.builder(
          itemBuilder: (context, index) {
            return Card(
                child: Column(
                  children: <Widget>[
                    Text(_dataClass[index].title),
                    Text( _dataClass[index].year),
                    Text(_dataClass[index].category),
                  ],
              ),
            );
          },
          itemCount: _dataClass.length,
       ),
    );
  }
}

共有3个答案

施锋
2023-03-14

而不是

for(var dataJson in datasJson){
        datas.add(DataClass.fromJson(dataJson));
}

用这个

for (var dataJson in datasJson['data']) {
    datas.add(DataClass.fromJson(dataJson));
}
郎长卿
2023-03-14

在您的示例中,datasJson是一个带有data键的映射。您想要遍历分配给data键的列表:

var datasJson = json.decode(response.body)['data'];
柴衡
2023-03-14

正如@hacker1024所说,你在那里丢失了钥匙。

如果您是从Api获取数据,请尝试使用FutureBuilder。

我正在从这里的资产文件夹加载(别忘了添加pubspec.yaml

Future<List<DataClass>> getMovieData() async {
    final myJson = await rootBundle.loadString("assets/j.json");
    // var response = await http.get(Uri.parse(myJson));

    // if (response.statusCode == 200) {
    final datasJson = json.decode(myJson)["data"] as List;

    return datasJson.map((js) => DataClass.fromJson(js)).toList();
    // } else
    //   print("http error");
    // return [];
  }

以及如何实施

@override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.cyan,
      body: FutureBuilder<List<DataClass>>(
          future: getMovieData(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting)
              return CircularProgressIndicator();

            if (snapshot.hasError)
              return Text(snapshot.error.toString());
            else if (snapshot.hasData &&
                snapshot.connectionState == ConnectionState.done)
              return ListView.builder(
                itemBuilder: (context, index) {
                  return Card(
                    child: Column(
                      children: <Widget>[
                        Text(snapshot.data![index].title),
                        Text(snapshot.data![index].year),
                        Text(snapshot.data![index].category),
                      ],
                    ),
                  );
                },
                itemCount: snapshot.data!.length,
              );
            else
              return Text("impliment more");
          }),
    );
  }
 类似资料: