Home > Back-end >  How to get value from JSON in flutter
How to get value from JSON in flutter

Time:11-04

I am trying to append some fake data to Listview.builder for category display, but i don't want get data from API, so i just create my own json in dart

final category = {
          "Table": "Category",
          "Result": [
            {"Name": "Pants", "id": 1, "image": "cat1.png"},
            {"Name": "T Shirts", "id": 2, "image": "cat2.png"},
            {"Name": "Watches", "id": 3, "image": "cat3.png"}
          ]
  };

I have create two classes also

class CategoryList {
  String table;
  ResultCategory result;

  CategoryList(this.table, this.result);
}

class ResultCategory {
  String name;
  String id;
  String image;

  ResultCategory(this.name, this.id, this.image);
}

So my question is how to append this data to Listview.builder

What i have done is

Future<List<CategoryList>> _getCategory() async {
    final category = {
      "Table": "Category",
      "Result": [
        {"Name": "Pants", "id": 1, "image": "cat1.png"},
        {"Name": "T Shirts", "id": 2, "image": "cat2.png"},
        {"Name": "Watches", "id": 3, "image": "cat3.png"}
      ]
    };


    Map<String, dynamic> caregoryRes = json.decode(category);

    List<CategoryList> categorys = [];
    CategoryList categoryList =  CategoryList(category.Table, category["Result"]);

    categorys.add(categoryList);

    return categorys;
  }

CodePudding user response:

To load and display data you have to use enter image description here

  • Related