I am trying to write a function that will parse the json file and show me the related infor like Sid
, Star
and Approach
, for now this is the json file , my main file has buttons that will load a list view for the options i select,
{
"@ICAOcode": "VNJP",
"Sid": [
{
"@Name": "DAMA1A",
"@Runways": "27",
"Sid_Waypoint": [
{
"@ID": "1",
"Name": "(2000)",
"Type": "ConstHdgtoAlt",
"Latitude": "0.000000",
"Longitude": "0.000000",
"Speed": "0",
"Altitude": "2000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Hdg_Crs": "0",
"Hdg_Crs_value": "271",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "LALBA",
"Type": "Normal",
"Latitude": "26.947222",
"Longitude": "85.806389",
"Speed": "0",
"Altitude": "7500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Right"
},
{
"@ID": "3",
"Name": "DAMAR",
"Type": "Normal",
"Latitude": "27.255850",
"Longitude": "85.662892",
"Speed": "0",
"Altitude": "12500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
}
]
},
{
"@Name": "DAMA1B",
"@Runways": "09",
"Sid_Waypoint": [
{
"@ID": "1",
"Name": "(2000)",
"Type": "ConstHdgtoAlt",
"Latitude": "0.000000",
"Longitude": "0.000000",
"Speed": "0",
"Altitude": "2000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Hdg_Crs": "0",
"Hdg_Crs_value": "091",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "LALBA",
"Type": "Normal",
"Latitude": "26.947222",
"Longitude": "85.806389",
"Speed": "0",
"Altitude": "7500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Left"
},
{
"@ID": "3",
"Name": "DAMAR",
"Type": "Normal",
"Latitude": "27.255850",
"Longitude": "85.662892",
"Speed": "0",
"Altitude": "12500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
}
]
},
{
"@Name": "THIL1A",
"@Runways": "09",
"Sid_Waypoint": [
{
"@ID": "1",
"Name": "(1000)",
"Type": "ConstHdgtoAlt",
"Latitude": "0.000000",
"Longitude": "0.000000",
"Speed": "0",
"Altitude": "1000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Hdg_Crs": "0",
"Hdg_Crs_value": "091",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "THILA",
"Type": "Normal",
"Latitude": "26.790619",
"Longitude": "86.095494",
"Speed": "0",
"Altitude": "3000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Left"
}
]
}
],
"Star": [
{
"@Name": "DAMA1C",
"@Runways": "09",
"Star_Waypoint": [
{
"@ID": "1",
"Name": "DAMAR",
"Type": "Normal",
"Latitude": "27.255850",
"Longitude": "85.662892",
"Speed": "0",
"Altitude": "13500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "LALBA",
"Type": "Normal",
"Latitude": "26.947222",
"Longitude": "85.806389",
"Speed": "0",
"Altitude": "7500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "3",
"Name": "MEGHA",
"Type": "Normal",
"Latitude": "26.882486",
"Longitude": "85.836403",
"Speed": "0",
"Altitude": "6000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
}
]
},
{
"@Name": "DAMA1D",
"@Runways": "27",
"Star_Waypoint": [
{
"@ID": "1",
"Name": "DAMAR",
"Type": "Normal",
"Latitude": "27.255850",
"Longitude": "85.662892",
"Speed": "0",
"Altitude": "13500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "LALBA",
"Type": "Normal",
"Latitude": "26.947222",
"Longitude": "85.806389",
"Speed": "180",
"Altitude": "7500",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "3",
"Name": "THILA",
"Type": "Normal",
"Latitude": "26.790619",
"Longitude": "86.095494",
"Speed": "180",
"Altitude": "3000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "4",
"Name": "DHABA",
"Type": "Normal",
"Latitude": "26.707047",
"Longitude": "86.095917",
"Speed": "0",
"Altitude": "3000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
}
]
}
],
"Approach": {
"@Name": "RNV09",
"App_Waypoint": [
{
"@ID": "1",
"Name": "JP962",
"Type": "Normal",
"Latitude": "26.710039",
"Longitude": "85.785028",
"Speed": "0",
"Altitude": "2200",
"AltitudeCons": "0",
"AltitudeRestriction": "at",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "JP961",
"Type": "Normal",
"Latitude": "26.709417",
"Longitude": "85.855842",
"Speed": "0",
"Altitude": "1300",
"AltitudeCons": "0",
"AltitudeRestriction": "at",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "3",
"Name": "RW09",
"Type": "Runway",
"Latitude": "26.708869",
"Longitude": "85.915303",
"Speed": "0",
"Altitude": "286",
"AltitudeCons": "0",
"AltitudeRestriction": "at",
"Flytype": "Fly-over",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "4",
"Name": "DHABA",
"Type": "Normal",
"Latitude": "26.707047",
"Longitude": "86.095917",
"Speed": "0",
"Altitude": "3000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "5",
"Name": "DHABA",
"Type": "Hold",
"Latitude": "26.707047",
"Longitude": "86.095917",
"Speed": "160",
"Altitude": "3000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Hld_Rad_or_Inbd": "Inbd",
"Hld_Rad_value": "266",
"Hld_Turn": "Right",
"Hld_Time_or_Dist": "Time",
"Hld_td_value": "1.0",
"Sp_Turn": "Right"
}
],
"App_Transition": {
"@Name": "MEGHA",
"AppTr_Waypoint": [
{
"@ID": "1",
"Name": "MEGHA",
"Type": "Normal",
"Latitude": "26.882486",
"Longitude": "85.836403",
"Speed": "0",
"Altitude": "6000",
"AltitudeCons": "0",
"AltitudeRestriction": "above",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
},
{
"@ID": "2",
"Name": "JP963",
"Type": "Normal",
"Latitude": "26.793614",
"Longitude": "85.784606",
"Speed": "0",
"Altitude": "3800",
"AltitudeCons": "0",
"AltitudeRestriction": "at",
"Flytype": "Fly-by",
"BankLimit": "25",
"Sp_Turn": "Auto"
}
]
}
}
}
This is the code i have written so far to parse it but i get errors while trying to parse Approach
The errors vary the latest one is [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
at this line for (final app in decodedData['Approach'].values) {
in the below code
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:flutter/services.dart' show rootBundle;
class ProcedureProvider extends ChangeNotifier {
Future<List<ProcRoute>> extractAirportData(String filename) async {
final String jsonData = await rootBundle.loadString(filename);
final decodedData = json.decode(jsonData);
final List<ProcRoute> routes = [];
for (final sid in decodedData['Sid']) {
final List<Waypoint> waypoints = [];
for (final wp in sid['Sid_Waypoint']) {
waypoints.add(Waypoint(
wp['Name'],
wp['Type'],
double.parse(wp['Latitude']),
double.parse(wp['Longitude']),
double.parse(wp['Altitude']),
wp['AltitudeRestriction'],
wp['Flytype'],
));
}
routes.add(ProcRoute(sid['@Name'], sid['@Runways'], waypoints));
}
for (final star in decodedData['Star']) {
final List<Waypoint> waypoints = [];
for (final wp in star['Star_Waypoint']) {
waypoints.add(Waypoint(
wp['Name'],
wp['Type'],
double.parse(wp['Latitude']),
double.parse(wp['Longitude']),
double.parse(wp['Altitude']),
wp['AltitudeRestriction'],
wp['Flytype'],
));
}
routes.add(ProcRoute(star['@Name'], star['@Runways'], waypoints));
}
for (final app in decodedData['Approach'].values) {
final List<Waypoint> waypoints = [];
for (final wp in app['App_Waypoint']) {
waypoints.add(Waypoint(
wp['Name'],
wp['Type'],
double.parse(wp['Latitude']),
double.parse(wp['Longitude']),
double.parse(wp['Altitude']),
wp['AltitudeRestriction'],
wp['Flytype'],
));
}
routes.add(ProcRoute(app['@Name'], app['@Runways'], waypoints));
}
for (final appTrans in decodedData['App_Transition']) {
final List<Waypoint> waypoints = [];
for (final wp in appTrans['App_Transition_Waypoint']) {
waypoints.add(Waypoint(
wp['Name'],
wp['Type'],
double.parse(wp['Latitude']),
double.parse(wp['Longitude']),
double.parse(wp['Altitude']),
wp['AltitudeRestriction'],
wp['Flytype'],
));
}
routes.add(ProcRoute(appTrans['@Name'], appTrans['@Runways'], waypoints));
}
return routes;
}
List<String> getSids(List<ProcRoute> routes) {
final sids = <String>[];
for (final route in routes) {
if (route.name.startsWith('SID')) {
sids.add(route.name);
}
}
return sids;
}
}
class Waypoint {
final String name;
final String type;
final double latitude;
final double longitude;
final double altitude;
final String altitudeRestriction;
final String flyType;
Waypoint(this.name, this.type, this.latitude, this.longitude, this.altitude,
this.altitudeRestriction, this.flyType);
}
class ProcRoute {
final String name;
final String runway;
final List<Waypoint> waypoints;
ProcRoute(this.name, this.runway, this.waypoints);
}
How do i fix it ?
2
Answers
You can try using this class to decode your data:
Use it like this:
You can use this online tool to create Model classes like the one above using json data.
why Sid, Star are Arrays in yout JSON file, and Approach isn’t?
https://www.edureka.co/community/83636/how-to-use-for-loop-in-flutter
For in loop "It is mainly used to iterate over array’s or collection’s elements."
You should write a factory function ‘fromJson’ in your Waypoint class, to
get your Code cleaner and smaller.