'Flutter nested json importing from server to sqlite gives error

This is my JSON:

[
        {
     "date": "2022-04-05",
        "graph": “graphics image”,
        "events": [
            {
                "id": "337463",
                "counter": 1,
                "creation_time": "05:30 AM",
                "creation_date": "2022-04-05 05:30:00",
                "previous_time": "05:45 AM",
                "previous_date": "2022-04-05 05:45:00",
                "duration": "00 hr 15 min 00 sec",
                "address": "",
                "comments": "PTI",
                "mode_name": "On",
                "mode_number": "5",
                "timestamp": "2022-04-06 02:53:46",
                "date": "2022-04-05"
            },
            {
                "id": "337466",
                "counter": 2,
                "creation_time": "05:45 AM",
                "creation_date": "2022-04-05 05:45:00",
                "previous_time": "06:22 AM",
                "previous_date": "2022-04-05 06:22:00",
                "duration": "00 hr 37 min 00 sec",
                "address": "400 Ewen Ave, New Westminster, V3M, BC, CAN",
                "comments": "",
                "mode_name": "Dr",
                "mode_number": "15",
                "timestamp": "2022-04-06 02:53:46",
                "date": "2022-04-05"
            },
            {
                "id": "337468",
                "counter": 3,
                "creation_time": "06:22 AM",
                "creation_date": "2022-04-05 06:22:00",
                "previous_time": "06:36 AM",
                "previous_date": "2022-04-05 06:36:00",
                "duration": "00 hr 14 min 00 sec",
                "address": "S Fraser Perimeter Rd, Surrey, V3R, BC, CAN",
                "comments": "",
                "mode_name": "On",
                "mode_number": "5",
                "timestamp": "2022-04-06 02:53:46",
                "date": "2022-04-05"
            },
            {
                "id": "337469",
                "counter": 4,
                "creation_time": "06:36 AM",
                "creation_date": "2022-04-05 06:36:00",
                "previous_time": "06:45 AM",
                "previous_date": "2022-04-05 06:45:00",
                "duration": "00 hr 09 min 00 sec",
                "address": "10960-10960 168 St, Surrey, V4N, BC, CAN",
                "comments": "",
                "mode_name": "Dr",
                "mode_number": "15",
                "timestamp": "2022-04-06 02:53:46",
                "date": "2022-04-05"
            },
    
        ],
        "truck": [
            {
                "id": "13",
                "truck_number": "A114",
                "truck_make": "KENWORTH",
                "truck_year": "2008",
                "truck_owner": "ABC",
                "vin_number": "008J936777",
                "registration_number": "00245",
                "under_company": "ABC Freight Ltd",
                "date": "2022-04-05"
            }
        ],
        "chassis": [
            {
                "date": "2022-04-05",
                "id": "31",
                "name": "321"
            },
            {
                "date": "2022-04-05",
                "id": "42",
                "name": "327"
            },
            {
                "date": "2022-04-05",
                "id": "11",
                "name": "20BT5"
            },
            {
                "date": "2022-04-05",
                "id": "46",
                "name": "32BT5"
            },
            {
                "date": "2022-04-05",
                "id": "126",
                "name": "453XG2"
            }
        ],
        "driver": [
            {
                "id": "1",
                "first_name": "Lori",
                "last_name": "TH",
                "email": "[email protected]",
                "phone": "00000000",
                "wcb": "ABC",
                "sin": "",
                "licence": "70236326249",
                "licence_expire": "2016-02-23",
                "portpass": "50543391",
                "pass_expiry": "2024-05-22",
                "date": "2022-04-17",
                "token": "Ly9WK_OSZjWL0d",
                "image": null,
                "driverid": "120",
                "current_state": "Active"
            }
        ]
    },
    {
        "date": "2022-04-05",
        "graph": “graphics image”,
        "events": [
            {
                "id": "337463",
                "counter": 1,
                "creation_time": "05:30 AM",
                "creation_date": "2022-04-05 05:30:00",
                "previous_time": "05:45 AM",
                "previous_date": "2022-04-05 05:45:00",
                "duration": "00 hr 15 min 00 sec",
                "address": "",
                "comments": "PTI",
                "mode_name": "On",
                "mode_number": "5",
                "timestamp": "2022-04-06 02:53:46",
                "date": "2022-04-05"
            },
            {
                "id": "337466",
                "counter": 2,
                "creation_time": "05:45 AM",
                "creation_date": "2022-04-05 05:45:00",
                "previous_time": "06:22 AM",
                "previous_date": "2022-04-05 06:22:00",
                "duration": "00 hr 37 min 00 sec",
                "address": "400 Ewen Ave, New Westminster, V3M, BC, CAN",
                "comments": "",
                "mode_name": "Dr",
                "mode_number": "15",
                "timestamp": "2022-04-06 02:53:46",
                "date": "2022-04-05"
            },
            {
                "id": "337468",
                "counter": 3,
                "creation_time": "06:22 AM",
                "creation_date": "2022-04-05 06:22:00",
                "previous_time": "06:36 AM",
                "previous_date": "2022-04-05 06:36:00",
                "duration": "00 hr 14 min 00 sec",
                "address": "S Fraser Perimeter Rd, Surrey, V3R, BC, CAN",
                "comments": "",
                "mode_name": "On",
                "mode_number": "5",
                "timestamp": "2022-04-06 02:53:46",
                "date": "2022-04-05"
            },
            {
                "id": "337469",
                "counter": 4,
                "creation_time": "06:36 AM",
                "creation_date": "2022-04-05 06:36:00",
                "previous_time": "06:45 AM",
                "previous_date": "2022-04-05 06:45:00",
                "duration": "00 hr 09 min 00 sec",
                "address": "10960-10960 168 St, Surrey, V4N, BC, CAN",
                "comments": "",
                "mode_name": "Dr",
                "mode_number": "15",
                "timestamp": "2022-04-06 02:53:46",
                "date": "2022-04-05"
            },
    
        ],
        "truck": [
            {
                "id": "13",
                "truck_number": "A114",
                "truck_make": "KENWORTH",
                "truck_year": "2008",
                "truck_owner": "ABC",
                "vin_number": "008J936777",
                "registration_number": "00245",
                "under_company": "ABC Freight Ltd",
                "date": "2022-04-05"
            }
        ],
        "chassis": [
            {
                "date": "2022-04-05",
                "id": "31",
                "name": "321"
            },
            {
                "date": "2022-04-05",
                "id": "42",
                "name": "327"
            },
            {
                "date": "2022-04-05",
                "id": "11",
                "name": "20BT5"
            },
            {
                "date": "2022-04-05",
                "id": "46",
                "name": "32BT5"
            },
            {
                "date": "2022-04-05",
                "id": "126",
                "name": "453XG2"
            }
        ],
        "driver": [
            {
                "id": "1",
                "first_name": "Lori",
                "last_name": "TH",
                "email": "[email protected]",
                "phone": "00000000",
                "wcb": "ABC",
                "sin": "",
                "licence": "70236326249",
                "licence_expire": "2016-02-23",
                "portpass": "50543391",
                "pass_expiry": "2024-05-22",
                "date": "2022-04-17",
                "token": "Ly9WK_OSZjWL0d",
                "image": null,
                "driverid": "120",
                "current_state": "Active"
            }
        ]
    },
]

This is my model class:

class EldDatajson {
  String date;
  String graph;
  List<EldlistData> events;
  List<EldDriver> driver;
  List<EldChassis> chassis;
  List<EldDTruck> truck;

  EldDatajson(this.date, this.graph, this.events, this.driver, this.chassis,
      this.truck);

  EldDatajson.fromJson(Map<String, dynamic> json)
      : date = json['date'],
        graph = json['graph'],
        events = (json['events'][0] as List)
            .map((i) => EldlistData.fromJson(i))
            .toList(),
        driver = (json['driver'][0] as List)
            .map((i) => EldDriver.fromJson(i))
            .toList(),
        chassis = (json['chassis'][0] as List)
            .map((i) => EldChassis.fromJson(i))
            .toList(),
        truck = (json['truck'][0] as List)
            .map((i) => EldDTruck.fromJson(i))
            .toList();

  Map<String, dynamic> toJson() => {
        'date': date,
        'graph': graph,
        'events': events,
        'driver': driver,
        'chassis': chassis,
        'truck': truck,
      };
}

List<EldDriver> driverFromJson(String str) =>
    List<EldDriver>.from(json.decode(str).map((x) => EldDriver.fromJson(x)));

String driverToJson(List<EldDriver> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class EldDriver {
  final int id;
  final String first_name;
  final String last_name;
  final String email;
  final String phone;
  final String wcb;
  final String sin;
  final String licence;
  final String licence_expire;
  final String portpass;
  final String pass_expiry;
  final String date;
  final String token;
  final int driverid;
  final String image;
  final String current_state;

  EldDriver(
      {required this.id,
      required this.first_name,
      required this.last_name,
      required this.email,
      required this.phone,
      required this.wcb,
      required this.sin,
      required this.licence,
      required this.licence_expire,
      required this.portpass,
      required this.pass_expiry,
      required this.date,
      required this.token,
      required this.driverid,
      required this.image,
      required this.current_state});

  factory EldDriver.fromJson(Map<String, dynamic> jsonDMap) => EldDriver(
        id: jsonDMap['id'],
        first_name: jsonDMap['first_name'],
        last_name: jsonDMap['last_name'],
        email: jsonDMap['email'],
        phone: jsonDMap['phone'],
        wcb: jsonDMap['wcb'],
        sin: jsonDMap['sin'],
        licence: jsonDMap['licence'],
        licence_expire: jsonDMap['licence_expire'],
        portpass: jsonDMap['portpass'],
        pass_expiry: jsonDMap['pass_expiry'],
        date: jsonDMap['date'],
        token: jsonDMap['token'],
        driverid: jsonDMap['driverid'],
        image: jsonDMap['image'],
        current_state: jsonDMap['current_state'],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "first_name": first_name,
        "last_name": last_name,
        "email": email,
        "phone": phone,
        "wcb": wcb,
        "sin": sin,
        "licence": licence,
        "licence_expire": licence_expire,
        "portpass": portpass,
        "pass_expiry": pass_expiry,
        "date": date,
        "token": token,
        "driverid": driverid,
        "image": image,
        "current_state": current_state,
      };
}

class EldChassis {
  final String id;
  final String name;
  final String date;

  EldChassis({required this.id, required this.name, required this.date});

  factory EldChassis.fromJson(Map<String, dynamic> jsonCMap) => EldChassis(
      id: jsonCMap['id'], name: jsonCMap['name'], date: jsonCMap['date']);

  Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "date": date,
      };
}

class EldDTruck {
  final String id;
  final String truck_number;
  final String truck_make;
  final String truck_year;
  final String truck_owner;
  final String vin_number;
  final String registration_number;
  final String under_company;
  final String date;

  EldDTruck(
      {required this.id,
      required this.truck_number,
      required this.truck_make,
      required this.truck_year,
      required this.truck_owner,
      required this.vin_number,
      required this.registration_number,
      required this.under_company,
      required this.date});

  factory EldDTruck.fromJson(Map<String, dynamic> jsonTMap) => EldDTruck(
      id: jsonTMap['id'],
      truck_number: jsonTMap['truck_number'],
      truck_make: jsonTMap['truck_make'],
      truck_year: jsonTMap['truck_year'],
      truck_owner: jsonTMap['truck_owner'],
      vin_number: jsonTMap['vin_number'],
      registration_number: jsonTMap['registration_number'],
      under_company: jsonTMap['under_company'],
      date: jsonTMap['date']);

  Map<String, dynamic> toJson() => {
        "id": id,
        "truck_number": truck_number,
        "truck_make": truck_make,
        "truck_year": truck_year,
        "truck_owner": truck_owner,
        "vin_number": vin_number,
        "registration_number": registration_number,
        "under_company": under_company,
        "date": date,
      };
}

I am not able to process json, I want to store this JSON to SQLite and then retrieve it back for offline use later on.

Error I am getting:

_TypeError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String')


Solution 1:[1]

In the following line:

EldDatajson.fromJson(Map<String, dynamic> json) 

remove Map<......> and use only dynamic keyword with specifying data type.

Like this: EldDatajson.fromJson (dynamic json). Use the same way for all constructors in your models.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 halfer