Home > Enterprise >  Impossible to get datas from Json Flutter
Impossible to get datas from Json Flutter

Time:05-28

In my Flutter project i would like to get datas from an API.

I used QuickType to generate my classes to parse my Json.

This error is always occured:

_TypeError (type 'List' is not a subtype of type 'String')

This my API response:

    [
{
"id": 1,
"label": "Animals",
"created_at": "2022-05-25T13:36:42.517Z",
"updated_at": "2022-05-25T13:36:42.517Z",
"sub_categories": [
{
"id": 2,
"label": "Ant",
"category": 1,
"published_at": "2022-05-25T13:40:48.172Z",
"created_at": "2022-05-25T13:40:48.174Z",
"updated_at": "2022-05-25T16:29:58.047Z"
},
{
"id": 3,
"label": "Cats",
"category": 1,
"published_at": "2022-05-25T13:42:07.022Z",
"created_at": "2022-05-25T13:42:07.023Z",
"updated_at": "2022-05-25T16:30:08.576Z"
},
{
"id": 4,
"label": "Dogs",
"category": 1,
"published_at": "2022-05-25T13:42:12.270Z",
"created_at": "2022-05-25T13:42:12.271Z",
"updated_at": "2022-05-25T16:30:14.262Z"
}
]
},
{
"id": 2,
"label": "Art",
"created_at": "2022-05-25T13:36:45.994Z",
"updated_at": "2022-05-25T13:36:45.994Z",
"sub_categories": [
{
"id": 7,
"label": "Paint",
"category": 2,
"published_at": "2022-05-28T08:01:46.941Z",
"created_at": "2022-05-28T08:01:46.943Z",
"updated_at": "2022-05-28T08:01:46.948Z"
}
]
},
{
"id": 3,
"label": "Astrology",
"created_at": "2022-05-25T13:36:49.498Z",
"updated_at": "2022-05-25T13:36:49.498Z",
"sub_categories": [
{
"id": 8,
"label": "Taurus",
"category": 3,
"published_at": "2022-05-28T08:02:07.520Z",
"created_at": "2022-05-28T08:02:07.521Z",
"updated_at": "2022-05-28T08:02:07.526Z"
}
]
},
{
"id": 4,
"label": "Astronomy",
"created_at": "2022-05-25T13:36:54.035Z",
"updated_at": "2022-05-25T13:36:54.035Z",
"sub_categories": [
{
"id": 9,
"label": "Hubble",
"category": 4,
"published_at": "2022-05-28T08:02:14.747Z",
"created_at": "2022-05-28T08:02:14.748Z",
"updated_at": "2022-05-28T08:02:14.753Z"
}
]
},
{
"id": 5,
"label": "Cook",
"created_at": "2022-05-25T13:37:34.930Z",
"updated_at": "2022-05-25T13:37:34.930Z",
"sub_categories": [
{
"id": 5,
"label": "Plancha",
"category": 5,
"published_at": "2022-05-25T13:42:26.369Z",
"created_at": "2022-05-25T13:42:26.370Z",
"updated_at": "2022-05-25T16:30:23.512Z"
},
{
"id": 6,
"label": "Bakery",
"category": 5,
"published_at": "2022-05-25T16:31:49.887Z",
"created_at": "2022-05-25T16:31:49.890Z",
"updated_at": "2022-05-25T16:31:49.900Z"
}
]
},
{
"id": 6,
"label": "Crypto Currencies",
"created_at": "2022-05-25T13:37:43.113Z",
"updated_at": "2022-05-25T13:37:43.113Z",
"sub_categories": [
{
"id": 10,
"label": "Bitcoin",
"category": 6,
"published_at": "2022-05-28T08:02:52.814Z",
"created_at": "2022-05-28T08:02:52.815Z",
"updated_at": "2022-05-28T08:02:52.821Z"
}
]
},
{
"id": 7,
"label": "DIY",
"created_at": "2022-05-25T13:37:48.652Z",
"updated_at": "2022-05-25T13:37:48.652Z",
"sub_categories": [
{
"id": 11,
"label": "Tools",
"category": 7,
"published_at": "2022-05-28T08:03:05.030Z",
"created_at": "2022-05-28T08:03:05.031Z",
"updated_at": "2022-05-28T08:03:05.038Z"
}
]
},
{
"id": 8,
"label": "Ecology",
"created_at": "2022-05-25T13:37:53.776Z",
"updated_at": "2022-05-25T13:37:53.776Z",
"sub_categories": [
{
"id": 12,
"label": "Planet",
"category": 8,
"published_at": "2022-05-28T08:03:19.915Z",
"created_at": "2022-05-28T08:03:19.916Z",
"updated_at": "2022-05-28T08:03:19.922Z"
}
]
},
{
"id": 9,
"label": "Economy",
"created_at": "2022-05-25T13:37:59.384Z",
"updated_at": "2022-05-25T13:37:59.384Z",
"sub_categories": [
{
"id": 13,
"label": "Wall Street",
"category": 9,
"published_at": "2022-05-28T08:03:41.942Z",
"created_at": "2022-05-28T08:03:41.943Z",
"updated_at": "2022-05-28T08:03:41.948Z"
}
]
},
{
"id": 10,
"label": "Literature",
"created_at": "2022-05-25T13:38:07.354Z",
"updated_at": "2022-05-25T13:38:07.354Z",
"sub_categories": [
{
"id": 14,
"label": "Zola",
"category": 10,
"published_at": "2022-05-28T08:03:59.314Z",
"created_at": "2022-05-28T08:03:59.316Z",
"updated_at": "2022-05-28T08:03:59.322Z"
}
]
},
{
"id": 11,
"label": "Mode",
"created_at": "2022-05-25T13:38:32.915Z",
"updated_at": "2022-05-25T13:38:32.915Z",
"sub_categories": [
{
"id": 15,
"label": "Squirt",
"category": 11,
"published_at": "2022-05-28T08:04:10.245Z",
"created_at": "2022-05-28T08:04:10.246Z",
"updated_at": "2022-05-28T08:08:02.312Z"
}
]
},
{
"id": 12,
"label": "Movies",
"created_at": "2022-05-25T13:38:36.502Z",
"updated_at": "2022-05-25T13:38:36.502Z",
"sub_categories": [
{
"id": 16,
"label": "Tarantino",
"category": 12,
"published_at": "2022-05-28T08:04:48.519Z",
"created_at": "2022-05-28T08:04:48.520Z",
"updated_at": "2022-05-28T08:04:48.526Z"
}
]
},
{
"id": 13,
"label": "Music",
"created_at": "2022-05-25T13:38:41.227Z",
"updated_at": "2022-05-25T13:38:41.227Z",
"sub_categories": [
{
"id": 17,
"label": "Rock",
"category": 13,
"published_at": "2022-05-28T08:05:20.575Z",
"created_at": "2022-05-28T08:05:20.576Z",
"updated_at": "2022-05-28T08:05:20.580Z"
}
]
},
{
"id": 14,
"label": "Nature",
"created_at": "2022-05-25T13:38:46.772Z",
"updated_at": "2022-05-25T13:38:46.772Z",
"sub_categories": [
{
"id": 18,
"label": "Forest",
"category": 14,
"published_at": "2022-05-28T08:05:32.566Z",
"created_at": "2022-05-28T08:05:32.567Z",
"updated_at": "2022-05-28T08:05:32.572Z"
}
]
},
{
"id": 15,
"label": "Politic",
"created_at": "2022-05-25T13:38:52.016Z",
"updated_at": "2022-05-25T13:38:52.016Z",
"sub_categories": [
{
"id": 19,
"label": "Vote",
"category": 15,
"published_at": "2022-05-28T08:05:38.045Z",
"created_at": "2022-05-28T08:05:38.046Z",
"updated_at": "2022-05-28T08:05:38.052Z"
}
]
},
{
"id": 16,
"label": "Sciences",
"created_at": "2022-05-25T13:38:58.047Z",
"updated_at": "2022-05-25T13:38:58.047Z",
"sub_categories": [
{
"id": 20,
"label": "Chemistry",
"category": 16,
"published_at": "2022-05-28T08:05:51.520Z",
"created_at": "2022-05-28T08:05:51.521Z",
"updated_at": "2022-05-28T08:05:51.528Z"
},
{
"id": 21,
"label": "Chemistery",
"category": 16,
"published_at": "2022-05-28T08:05:54.625Z",
"created_at": "2022-05-28T08:05:54.627Z",
"updated_at": "2022-05-28T08:05:54.631Z"
}
]
},
{
"id": 17,
"label": "Series",
"created_at": "2022-05-25T13:39:02.864Z",
"updated_at": "2022-05-25T13:39:02.864Z",
"sub_categories": [
{
"id": 22,
"label": "GOT",
"category": 17,
"published_at": "2022-05-28T08:06:06.350Z",
"created_at": "2022-05-28T08:06:06.351Z",
"updated_at": "2022-05-28T08:06:06.355Z"
}
]
},
{
"id": 18,
"label": "Sexuality",
"created_at": "2022-05-25T13:39:10.901Z",
"updated_at": "2022-05-25T13:39:10.901Z",
"sub_categories": [
{
"id": 23,
"label": "Couple",
"category": 18,
"published_at": "2022-05-28T08:06:33.963Z",
"created_at": "2022-05-28T08:06:33.964Z",
"updated_at": "2022-05-28T08:06:33.968Z"
}
]
},
{
"id": 19,
"label": "Social Life",
"created_at": "2022-05-25T13:39:16.760Z",
"updated_at": "2022-05-25T13:39:16.760Z",
"sub_categories": [
{
"id": 24,
"label": "Interractions",
"category": 19,
"published_at": "2022-05-28T08:06:55.099Z",
"created_at": "2022-05-28T08:06:55.099Z",
"updated_at": "2022-05-28T08:06:55.104Z"
}
]
},
{
"id": 20,
"label": "Sport",
"created_at": "2022-05-25T13:39:21.296Z",
"updated_at": "2022-05-25T13:39:21.296Z",
"sub_categories": [
{
"id": 25,
"label": "Football",
"category": 20,
"published_at": "2022-05-28T08:07:13.972Z",
"created_at": "2022-05-28T08:07:13.973Z",
"updated_at": "2022-05-28T08:07:13.979Z"
}
]
},
{
"id": 21,
"label": "Theater",
"created_at": "2022-05-25T13:39:26.063Z",
"updated_at": "2022-05-25T13:39:26.063Z",
"sub_categories": [
{
"id": 26,
"label": "Molière",
"category": 21,
"published_at": "2022-05-28T08:07:22.217Z",
"created_at": "2022-05-28T08:07:22.219Z",
"updated_at": "2022-05-28T08:07:22.224Z"
}
]
},
{
"id": 22,
"label": "Vehicules",
"created_at": "2022-05-25T13:39:30.859Z",
"updated_at": "2022-05-25T13:39:30.859Z",
"sub_categories": [
{
"id": 27,
"label": "Audi",
"category": 22,
"published_at": "2022-05-28T08:07:30.514Z",
"created_at": "2022-05-28T08:07:30.515Z",
"updated_at": "2022-05-28T08:07:30.522Z"
}
]
},
{
"id": 23,
"label": "Video Games",
"created_at": "2022-05-25T13:39:36.926Z",
"updated_at": "2022-05-25T13:39:36.926Z",
"sub_categories": [
{
"id": 28,
"label": "Elden Ring",
"category": 23,
"published_at": "2022-05-28T08:07:38.335Z",
"created_at": "2022-05-28T08:07:38.339Z",
"updated_at": "2022-05-28T08:07:38.343Z"
}
]
}
]

My classes generated by QuickType:

class Category {
  Category({
    required this.id,
    required this.label,
    required this.createdAt,
    required this.updatedAt,
    required this.subCategories,
  });

  int id;
  String label;
  DateTime createdAt;
  DateTime updatedAt;
  List<SubCategory> subCategories;

  factory Category.fromRawJson(String str) =>
      Category.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory Category.fromJson(Map<String, dynamic> json) => Category(
        id: json["id"],
        label: json["label"],
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
        subCategories: List<SubCategory>.from(
            json["sub_categories"].map((x) => SubCategory.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "label": label,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
        "sub_categories":
            List<dynamic>.from(subCategories.map((x) => x.toJson())),
      };
}

class SubCategory {
  SubCategory({
    required this.id,
    required this.label,
    required this.category,
    required this.publishedAt,
    required this.createdAt,
    required this.updatedAt,
  });

  int id;
  String label;
  int category;
  DateTime publishedAt;
  DateTime createdAt;
  DateTime updatedAt;

  factory SubCategory.fromRawJson(String str) =>
      SubCategory.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory SubCategory.fromJson(Map<String, dynamic> json) => SubCategory(
        id: json["id"],
        label: json["label"],
        category: json["category"],
        publishedAt: DateTime.parse(json["published_at"]),
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "label": label,
        "category": category,
        "published_at": publishedAt.toIso8601String(),
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
      };
}

And this is my code:

List<Category> categoryFromJson(String str) =>
      List<Category>.from(json.decode(str).map((x) => Category.fromJson(x)));

  String categoryToJson(List<Category> data) =>
      json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

  Future<List<Category>> getCategories() async {
    var response =
        await http.get(Uri.parse("http://192.168.0.10:1337/categories/"));

    if (response.statusCode == 200) {}
    var decodedData = jsonDecode(response.body);

    final category = categoryFromJson(decodedData);

    print(category);

    return category;
  }

Thank you for your awnsers :)

CodePudding user response:

API providing list of data, try using

Future<List<Category>?> loadData() async {
 //use http insted of `rootBundle`
  final response = await rootBundle.loadString("assets/j.json");

  var decodedData = jsonDecode(response) as List;
  final items = decodedData.map((e) => Category.fromJson(e)).toList();
  return items;
}

  • Related