skip to Main Content

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


  1. You can try using this class to decode your data:

    class Data {
        final String? icaOcode;
        final List<Sid>? sid;
        final List<Star>? star;
        final Approach? approach;
    
        Data({
            this.icaOcode,
            this.sid,
            this.star,
            this.approach,
        });
    
        factory Data.fromJson(Map<String, dynamic> json) => Data(
            icaOcode: json["@ICAOcode"],
            sid: json["Sid"] == null ? [] : List<Sid>.from(json["Sid"]!.map((x) => Sid.fromJson(x))),
            star: json["Star"] == null ? [] : List<Star>.from(json["Star"]!.map((x) => Star.fromJson(x))),
            approach: json["Approach"] == null ? null : Approach.fromJson(json["Approach"]),
        );
    
        Map<String, dynamic> toJson() => {
            "@ICAOcode": icaOcode,
            "Sid": sid == null ? [] : List<dynamic>.from(sid!.map((x) => x.toJson())),
            "Star": star == null ? [] : List<dynamic>.from(star!.map((x) => x.toJson())),
            "Approach": approach?.toJson(),
        };
    }
    
    class Approach {
        final String? name;
        final List<AppWaypoint>? appWaypoint;
        final AppTransition? appTransition;
    
        Approach({
            this.name,
            this.appWaypoint,
            this.appTransition,
        });
    
        factory Approach.fromJson(Map<String, dynamic> json) => Approach(
            name: json["@Name"],
            appWaypoint: json["App_Waypoint"] == null ? [] : List<AppWaypoint>.from(json["App_Waypoint"]!.map((x) => AppWaypoint.fromJson(x))),
            appTransition: json["App_Transition"] == null ? null : AppTransition.fromJson(json["App_Transition"]),
        );
    
        Map<String, dynamic> toJson() => {
            "@Name": name,
            "App_Waypoint": appWaypoint == null ? [] : List<dynamic>.from(appWaypoint!.map((x) => x.toJson())),
            "App_Transition": appTransition?.toJson(),
        };
    }
    
    class AppTransition {
        final String? name;
        final List<Waypoint>? appTrWaypoint;
    
        AppTransition({
            this.name,
            this.appTrWaypoint,
        });
    
        factory AppTransition.fromJson(Map<String, dynamic> json) => AppTransition(
            name: json["@Name"],
            appTrWaypoint: json["AppTr_Waypoint"] == null ? [] : List<Waypoint>.from(json["AppTr_Waypoint"]!.map((x) => Waypoint.fromJson(x))),
        );
    
        Map<String, dynamic> toJson() => {
            "@Name": name,
            "AppTr_Waypoint": appTrWaypoint == null ? [] : List<dynamic>.from(appTrWaypoint!.map((x) => x.toJson())),
        };
    }
    
    class Waypoint {
        final String? id;
        final String? name;
        final String? type;
        final String? latitude;
        final String? longitude;
        final String? speed;
        final String? altitude;
        final String? altitudeCons;
        final String? altitudeRestriction;
        final String? flytype;
        final String? bankLimit;
        final String? spTurn;
        final String? hdgCrs;
        final String? hdgCrsValue;
    
        Waypoint({
            this.id,
            this.name,
            this.type,
            this.latitude,
            this.longitude,
            this.speed,
            this.altitude,
            this.altitudeCons,
            this.altitudeRestriction,
            this.flytype,
            this.bankLimit,
            this.spTurn,
            this.hdgCrs,
            this.hdgCrsValue,
        });
    
        factory Waypoint.fromJson(Map<String, dynamic> json) => Waypoint(
            id: json["@ID"],
            name: json["Name"],
            type: json["Type"],
            latitude: json["Latitude"],
            longitude: json["Longitude"],
            speed: json["Speed"],
            altitude: json["Altitude"],
            altitudeCons: json["AltitudeCons"],
            altitudeRestriction: json["AltitudeRestriction"],
            flytype: json["Flytype"],
            bankLimit: json["BankLimit"],
            spTurn: json["Sp_Turn"],
            hdgCrs: json["Hdg_Crs"],
            hdgCrsValue: json["Hdg_Crs_value"],
        );
    
        Map<String, dynamic> toJson() => {
            "@ID": id,
            "Name": name,
            "Type": type,
            "Latitude": latitude,
            "Longitude": longitude,
            "Speed": speed,
            "Altitude": altitude,
            "AltitudeCons": altitudeCons,
            "AltitudeRestriction": altitudeRestriction,
            "Flytype": flytype,
            "BankLimit": bankLimit,
            "Sp_Turn": spTurn,
            "Hdg_Crs": hdgCrs,
            "Hdg_Crs_value": hdgCrsValue,
        };
    }
    
    class AppWaypoint {
        final String? id;
        final String? name;
        final String? type;
        final String? latitude;
        final String? longitude;
        final String? speed;
        final String? altitude;
        final String? altitudeCons;
        final String? altitudeRestriction;
        final String? flytype;
        final String? bankLimit;
        final String? spTurn;
        final String? hldRadOrInbd;
        final String? hldRadValue;
        final String? hldTurn;
        final String? hldTimeOrDist;
        final String? hldTdValue;
    
        AppWaypoint({
            this.id,
            this.name,
            this.type,
            this.latitude,
            this.longitude,
            this.speed,
            this.altitude,
            this.altitudeCons,
            this.altitudeRestriction,
            this.flytype,
            this.bankLimit,
            this.spTurn,
            this.hldRadOrInbd,
            this.hldRadValue,
            this.hldTurn,
            this.hldTimeOrDist,
            this.hldTdValue,
        });
    
        factory AppWaypoint.fromJson(Map<String, dynamic> json) => AppWaypoint(
            id: json["@ID"],
            name: json["Name"],
            type: json["Type"],
            latitude: json["Latitude"],
            longitude: json["Longitude"],
            speed: json["Speed"],
            altitude: json["Altitude"],
            altitudeCons: json["AltitudeCons"],
            altitudeRestriction: json["AltitudeRestriction"],
            flytype: json["Flytype"],
            bankLimit: json["BankLimit"],
            spTurn: json["Sp_Turn"],
            hldRadOrInbd: json["Hld_Rad_or_Inbd"],
            hldRadValue: json["Hld_Rad_value"],
            hldTurn: json["Hld_Turn"],
            hldTimeOrDist: json["Hld_Time_or_Dist"],
            hldTdValue: json["Hld_td_value"],
        );
    
        Map<String, dynamic> toJson() => {
            "@ID": id,
            "Name": name,
            "Type": type,
            "Latitude": latitude,
            "Longitude": longitude,
            "Speed": speed,
            "Altitude": altitude,
            "AltitudeCons": altitudeCons,
            "AltitudeRestriction": altitudeRestriction,
            "Flytype": flytype,
            "BankLimit": bankLimit,
            "Sp_Turn": spTurn,
            "Hld_Rad_or_Inbd": hldRadOrInbd,
            "Hld_Rad_value": hldRadValue,
            "Hld_Turn": hldTurn,
            "Hld_Time_or_Dist": hldTimeOrDist,
            "Hld_td_value": hldTdValue,
        };
    }
    
    class Sid {
        final String? name;
        final String? runways;
        final List<Waypoint>? sidWaypoint;
    
        Sid({
            this.name,
            this.runways,
            this.sidWaypoint,
        });
    
        factory Sid.fromJson(Map<String, dynamic> json) => Sid(
            name: json["@Name"],
            runways: json["@Runways"],
            sidWaypoint: json["Sid_Waypoint"] == null ? [] : List<Waypoint>.from(json["Sid_Waypoint"]!.map((x) => Waypoint.fromJson(x))),
        );
    
        Map<String, dynamic> toJson() => {
            "@Name": name,
            "@Runways": runways,
            "Sid_Waypoint": sidWaypoint == null ? [] : List<dynamic>.from(sidWaypoint!.map((x) => x.toJson())),
        };
    }
    
    class Star {
        final String? name;
        final String? runways;
        final List<Waypoint>? starWaypoint;
    
        Star({
            this.name,
            this.runways,
            this.starWaypoint,
        });
    
        factory Star.fromJson(Map<String, dynamic> json) => Star(
            name: json["@Name"],
            runways: json["@Runways"],
            starWaypoint: json["Star_Waypoint"] == null ? [] : List<Waypoint>.from(json["Star_Waypoint"]!.map((x) => Waypoint.fromJson(x))),
        );
    
        Map<String, dynamic> toJson() => {
            "@Name": name,
            "@Runways": runways,
            "Star_Waypoint": starWaypoint == null ? [] : List<dynamic>.from(starWaypoint!.map((x) => x.toJson())),
        };
    }
    
    

    Use it like this:

    final decodedData = json.decode(jsonData);
    final processedData = Data.fromJson(decodedData);
    

    You can use this online tool to create Model classes like the one above using json data.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search