0

Im trying to retrieve data from a Json. I have created a model as follows. Im receiving a response. When i try to model the response to a list it gives null. I've able to get the length of the list. But i cant access a value, it returns null

class Item {
  Item({
    this.sequenceno,
    this.id,
    this.chapter,
    this.title,
    this.packageDescription,
    this.ageLevel,
    this.pkgSequence,
    this.packagescors,
  });

  int? sequenceno;
  String? id;
  String? chapter;
  String? title;
  String? packageDescription;
  List<int>? ageLevel;
  String? pkgSequence;
  List<Packagescors>? packagescors;

  Item.fromMap(Map<String, dynamic> map) {
    Item(
      sequenceno: map["sequenceno"],
      id: map["_id"],
      chapter: map["chapter"],
      title: map["title"],
      packageDescription: map["package_description"],
      ageLevel: map["age_level"] != null
          ? List<int>.from(map["age_level"].map((x) => x))
          : [],
      pkgSequence: map["pkg_sequence"],
      packagescors: map["packagescors"] != null
          ? List<Packagescors>.from(
              map["device_details"].map((x) => Packagescors.fromMap(x)))
          : [],
    );
  }

  Map<String, dynamic> toMap() => {
        "sequenceno": sequenceno,
        "_id": id,
        "chapter": chapter,
        "title": title,
        "package_description": packageDescription,
        "age_level": ageLevel,
        "pkg_sequence": pkgSequence,
        "packagescors": packagescors,
      };
}

I'm making a api call as follows

Future<List<Item>> getmoduleList(
      String userId, String accountId) async {
    final response = await http
        .get(Uri.https(_baseUrl, "v1/package/module-list",queryParameters), headers: {
      'Content-Type': 'application/json',
      'user-id': userId,
      'account-id': accountId,
    });
    List<Item> jsonresponse = json.decode(response.body)['data']['items'].map<Item>((e)=>Item.fromMap(e)).toList();
    print('This is inside modulelist grade : ${jsonresponse[0].title}'); //here gets null
    print('This is inside modulelist grade : ${jsonresponse.length}'); //here receives length of the list
    return jsonresponse;
  }

Json response

 {
        "success": true,
        "data": {
            "items": [
                {
                    "sequenceno": 19,
                    "_id": "07",
                    "chapter": "Numbers",
                    "title": "Place Value: 3-digits",
                    "package_description": "familiarise with the concept of place value of 3-digit numbers.",
                    "age_level": [
                        99,
                        8
                    ],
                    "pkg_sequence": "25",
                    "packagescors": {
                        "score": [
                            50
                        ],
                        "date": [
                            "1600259121340"
                        ]
                    }
                }
    ]
   }
  }

Im able to get the length of the list but whenever tries to return a value it shows null. How can I solve this? What makes it returns null?

Febin Johnson
  • 277
  • 1
  • 6
  • 21

0 Answers0