【问题标题】:Flutter Json Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>'Flutter Json 未处理异常:类型“_InternalLinkedHashMap<String, dynamic>”不是“Iterable<dynamic>”类型的子类型
【发布时间】:2021-08-01 07:17:39
【问题描述】:

类类别 错误:未处理的异常:类型“_InternalLinkedHashMap”不是“Iterable”类型的子类型

  class Category {
  int id;
  String categoryName;
  String seoUrl;


  Category(this.id,this.categoryName,this.seoUrl);

  Category.fromJson(Map json){
    id = json["id"];
    categoryName = json["categoryName"];
    seoUrl = json["seoUrl"];
  }

  Map toJson(){
    return {
      "id": id,
      "categoryName": categoryName,
      "seoUrl": seoUrl
    };
  }
}

类主屏幕

错误:未处理的异常:类型“_InternalLinkedHashMap”不是类型“Iterable”的子类型


import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http_demo/data/api/category_api.dart';
import 'package:http_demo/models/category.dart';

class MainScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MainScreenState();
  }
}

class MainScreenState extends State {
  List<Category> categories = List<Category>();
  List<Widget> categoryWidgets = List<Widget>();

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Alışveriş Sistemi",
          style: TextStyle(color: Colors.white),
        ),
        backgroundColor: Colors.blueGrey,
        centerTitle: true,
      ),
      body: Padding(
        padding: EdgeInsets.all(10.0),
        child: Column(
          children: <Widget>[
            SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: Row(
                children: categoryWidgets,
              ),
            )
          ],
        ),
      ),
    );
  }

  void getCategoriesFromApi() {
    CategoryApi.getCategories().then((response) {
      setState(() {
        Iterable list = json.decode(response.body);
        //categories = list.map((category) => Category.fromJson(category)).toList();
        this.categories = List<Category>.from(list.map((model)=> Category.fromJson(model)));
        getCategoryWidgets();
      });
    });
  }

  List<Widget> getCategoryWidgets() {
    for (int i = 0; i < categories.length; i++) {
      categoryWidgets.add(getCategoryWidget(categories[i]));
    }
    return categoryWidgets;
  }

  Widget getCategoryWidget(Category category) {
    return FlatButton(
      onPressed: null,
      child: Text(
        category.categoryName,
        style: TextStyle(color: Colors.blueGrey),
      ),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(15.0),
        side: BorderSide(color: Colors.lightGreenAccent),
      ),
    );
  }
}

Error: Unhandled Exception: type '_InternalLinkedHashMap&lt;String, dynamic&gt;' is not a subtype of type 'Iterable&lt;dynamic&gt;' 错误:未处理的异常:类型“_InternalLinkedHashMap”不是“Iterable”类型的子类型 错误:未处理的异常:类型“_InternalLinkedHashMap”不是“Iterable”类型的子类型 错误:未处理的异常:类型“_InternalLinkedHashMap”不是“Iterable”类型的子类型 错误:未处理的异常:类型“_InternalLinkedHashMap”不是“Iterable”类型的子类型

【问题讨论】:

    标签: flutter


    【解决方案1】:

    请检查 JSON 转换后的响应。

    void getCategoriesFromApi() {
      CategoryApi.getCategories().then((response) {
        setState(() {
          var list = json.decode(response.body);
          //categories = list.map((category) => Category.fromJson(category)).toList();
          if (list is List) {
            this.categories = list.map((e) => Category.fromJson(e)).toList();
            getCategoryWidgets();
          } else {
            print('response is not list');
          }
        });
      });
    }
    

    【讨论】:

    • 可以分享一下response.body 吗?
    • 我得到了文本“response is not list” import 'package:http/http.dart' as http;类 CategoryApi{ 静态未来 getCategories(){ return http.get("10.0.2.2:3000/category"); } }
    • 是的。您可以检查响应。打印(response.body);
    • 类类别 { int id;字符串类别名称;字符串 seoUrl;类别(this.id,this.categoryName,this.seoUrl); Category.fromJson(Map json){ id = json["id"];类别名称 = json["类别名称"]; seoUrl = json["seoUrl"]; } Map toJson(){ return { "id": id, "categoryName": categoryName, "seoUrl": seoUrl }; } }
    • 我正在使用 bluestack。
    猜你喜欢
    • 2021-09-12
    • 2020-07-14
    • 2021-09-20
    • 2021-07-21
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 2020-11-25
    相关资源
    最近更新 更多