2

Class Category Error: Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type '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
    };
  }
}

Class Main Screen

Error: Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type '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<String, dynamic>' is not a subtype of type 'Iterable<dynamic>' Error: Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable' Error: Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable' Error: Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable' Error: Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable'

1 Answer 1

1

Please check the response after convert 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');
      }
    });
  });
}
Sign up to request clarification or add additional context in comments.

9 Comments

can you share the response.body ?
I got the text "response is not list" import 'package:http/http.dart' as http; class CategoryApi{ static Future getCategories(){ return http.get("10.0.2.2:3000/category"); } }
Yes. you can check the response. print(response.body);
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 }; } }
I'm using bluestack.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.