How to retrieve Json Data stored locally in flutter

Viewed 34

Here is my controller code

class PackageController extends GetxController {
  PackageController({Key? key});

  List<dynamic> _packageList = [];
  List _items = [];

  List<dynamic> get packageList => _packageList;

  void getPackageList() {
    _packageList = [];
    _packageList.addAll(PackageModel.fromJson(_items).packages);
  }

  Future<void> readJson() async {
    final String response = await rootBundle.loadString('local/packages.json');
    final data = await json.decode(response);
    _items = data["packages"];
    update();
  }
  //end
}

but am faced with this issue

enter image description here

Here is my Model that controls the fromJson data in the package controller of the project.

class PackageModel {
  PackageModel({
    required this.packages,
  });
  late final List<Packages> packages;

  PackageModel.fromJson(Map<String, dynamic> json) {
    packages =
        List.from(json['packages']).map((e) => Packages.fromJson(e)).toList();
  }
}

class Packages {
  Packages({
    required this.id,
    required this.name,
    required this.description,
    required this.price,
    required this.img,
  });
  late final int id;
  late final String name;
  late final String description;
  late final String price;
  late final String img;

  Packages.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    description = json['description'];
    price = json['price'];
    img = json['img'];
  }
}

Here is the json data

{
    "packages": [
        {
            "id": 1,
            "name": "VIP 1",
            "description": "",
            "price": "10",
            "img": ""
        }
    ]
}

I am trying how to retrieve Json Data stored locally in flutter but I ran into a problem, I need help

0 Answers
Related