i m trying to parse this json which print 'confirmed'
{ "Delhi": { "districtData": { "East Delhi": { "confirmed": 1, "lastupdatedtime": "" }, "South West Delhi": { "confirmed": 3, "lastupdatedtime": "" }, "West Delhi": { "confirmed": 2, "lastupdatedtime": "" }, "North Delhi": { "confirmed": 3, "lastupdatedtime": "" }, "New Delhi": { "confirmed": 3, "lastupdatedtime": "" }, "South Delhi": { "confirmed": 2, "lastupdatedtime": "" }, "North East Delhi": { "confirmed": 1, "lastupdatedtime": "" }, "North West Delhi": { "confirmed": 3, "lastupdatedtime": "" }, "Unknown": { "confirmed": 53, "lastupdatedtime": "" }, "Delhi": { "confirmed": 1, "lastupdatedtime": "" } } } } https://api.covid19india.org/state_district_wise.json is actual json data and deserialized only delhi data because i want to test it whether it is right or wrong. It is as
class IndiaState { Delhi delhi; IndiaState({ this.delhi, }); factory IndiaState.fromJson(Map<String, dynamic> json) => IndiaState( delhi: Delhi.fromJson(json["Delhi"]), ); } class Delhi { DelhiDistrictData districtData; Delhi({ this.districtData, }); factory Delhi.fromJson(Map<String, dynamic> json) => Delhi( districtData: DelhiDistrictData.fromJson(json["districtData"]), ); } class DelhiDistrictData { DelhiValue eastDelhi; DelhiValue southWestDelhi; DelhiValue westDelhi; DelhiValue delhi; DelhiValue southDelhi; DelhiValue northEastDelhi; DelhiValue northDelhi; DelhiValue northWestDelhi; DelhiValue unknown; DelhiValue newDelhi; DelhiDistrictData({ this.eastDelhi, this.southWestDelhi, this.westDelhi, this.delhi, this.southDelhi, this.northEastDelhi, this.northDelhi, this.northWestDelhi, this.unknown, this.newDelhi, }); factory DelhiDistrictData.fromJson(Map<String, dynamic> json) => DelhiDistrictData( eastDelhi: DelhiValue.fromJson(json["East Delhi"]), southWestDelhi: DelhiValue.fromJson(json["South West Delhi"]), westDelhi: DelhiValue.fromJson(json["West Delhi"]), delhi: DelhiValue.fromJson(json["Delhi"]), southDelhi: DelhiValue.fromJson(json["South Delhi"]), northEastDelhi: DelhiValue.fromJson(json["North East Delhi"]), northDelhi: DelhiValue.fromJson(json["North Delhi"]), northWestDelhi: DelhiValue.fromJson(json["North West Delhi"]), newDelhi: DelhiValue.fromJson(json["New Delhi"]), unknown: DelhiValue.fromJson(json["Unknown"]), ); } class DelhiValue { int confirmed; String lastupdatedtime; DelhiValue({ this.confirmed, this.lastupdatedtime, }); factory DelhiValue.fromJson(Map<String, dynamic> json) => DelhiValue( confirmed: json['confirmed'], lastupdatedtime: json["lastupdatedtime"], ); } now i am trying to print the confirmed result any one district of delhi as
@override void initState() { // TODO: implement initState setState(() { isLoading = true; }); givenFunction(); setState(() { isLoading = false; }); } Future givenFunction() async { final httpRequest = await http.get(districtAPI); final json = jsonDecode(httpRequest.body); IndiaState firstObject = new IndiaState.fromJson(json); print(firstObject.delhi.districtData.eastDelhi.confirmed.toString()); } Now here comes the error when i tried to print eastDelhi confirmed data
E/flutter ( 5895): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null. E/flutter ( 5895): Receiver: null E/flutter ( 5895): Tried calling: []("confirmed") E/flutter ( 5895): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5) E/flutter ( 5895): #1 new DelhiValue.fromJson (package:coraona2903/models/district_state.dart:77:20) E/flutter ( 5895): #2 new DelhiDistrictData.fromJson (package:coraona2903/models/district_state.dart:54:23) E/flutter ( 5895): #3 new Delhi.fromJson (package:coraona2903/models/district_state.dart:21:37) E/flutter ( 5895): #4 new IndiaState.fromJson (package:coraona2903/models/district_state.dart:9:18) E/flutter ( 5895): #5 Covid19ScreenState.givenFunction (package:coraona2903/screens/covid_19_screen.dart:37:34) E/flutter ( 5895): <asynchronous suspension> 