Search code examples
flutterdartpostmandata-modeling

How to get data collection from a nested list of a data collection from postman api to list<dynamic> in dart Flutter


I have a postman API call. but I don't know how to extract data from the app JSON value in the API call of postman API. Can somebody clarify me about the how to extract it in dart data model.

{
"deviceType": "andriod",
"deviceId": "C6179909526098",
"deviceName": "Samsung-MT200",
"deviceOSVersion": "2.3.6",
"deviceIPAddress": "11.433.445.66",
"lat": 9.9312,
"long": 76.2673,
"buyer_gcmid": "",
"buyer_pemid": "",
"app": {
    "version": "1.20.5",
    "installTimeStamp": "2022-02-10T12:33:30.696Z",
    "uninstallTimeStamp": "2022-02-10T12:33:30.696Z",
    "downloadTimeStamp": "2022-02-10T12:33:30.696Z"
}

}

this is the data model.

    class SplashPost {
  String? deviceType;
  String? deviceId;
  String? deviceName;
  String? deviceOSVerion;
  String? deviceIPAddress;
  String? lat;
  String? long;
  String? buyer_gcmid;
  String? buyer_pemid;
  String? version;
  List<dynamic>? app;

  SplashPost({
    required this.deviceType,
    required this.deviceIPAddress,
    required this.deviceId,
    required this.deviceName,
    required this.deviceOSVerion,
    required this.app,
    required this.buyer_gcmid,
    required this.buyer_pemid,
    required this.lat,
    required this.long,
    required this.version,
  });

  factory SplashPost.fromJson(Map<String, dynamic> json) => SplashPost(
        deviceIPAddress: json["deviceIPAddress"],
        deviceType: json["deviceType"],
        deviceId: json["deviceId"],
        deviceName: json["deviceName"],
        deviceOSVerion: json["deviceOSVersion"],
        lat: json["lat"],
        long: json["long"],
        version: json["version"],
        buyer_gcmid: json["buyer_gcmid"],
        buyer_pemid: json["buyer_pemid"],
        app: List<dynamic>.from(json["app"]).map((x) => x),
      );
}

Map<String, dynamic> toJson() => {
      "version": version,
      "installTimeStamp": installTimeStamp,
      "uninstallTimeStamp": uninstallTimeStamp,
      "downloadTimeStamp": downloadTimeStamp,
    };

This is the data model file and I don't know how to extract data from the "app" JSON value. It throws me the error of

The argument type 'Iterable' can't be assigned to the parameter type 'List?' in the list of app value.

and

Undefined name 'version'

Can somebody explain and solve my problem.


Solution

  • your json["app"] is of type Map<String, dynamic> json which is why List<dynamic>.from(json["app"]) will not work.

    you need to either change the type of app to:

    Map<String, dynamic> app;
    

    and parse it like

    factory SplashPost.fromJson(Map<String, dynamic> json) => SplashPost(
      // ... rest of code
      app: json['app'] ?? {}
    )
    

    or you can create a new class SplashPostApp

    and implement

    factory SplashPostApp.fromJson(Map<String, dynamic> json) ...
    

    then in your SplashPost class change type of app to SplashPostApp and parse it like:

    factory SplashPost.fromJson(Map<String, dynamic> json) => SplashPost(
    ...
    app: SplashPostApp.fromJson(json['app']?? {})