skip to Main Content

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.

2

Answers


  1. Personally I like using the website https://app.quicktype.io/ to generate a class from a response. It has some settings to tweak it to your liking and you can of course make adjustments yourself manually if the settings doesn’t cover it. Here is an example output from that website, using your response.

    import 'dart:convert';
    
    class SplashPost {
        String deviceType;
        String deviceId;
        String deviceName;
        String deviceOsVersion;
        String deviceIpAddress;
        double lat;
        double long;
        String buyerGcmid;
        String buyerPemid;
        App app;
    
        SplashPost({
            required this.deviceType,
            required this.deviceId,
            required this.deviceName,
            required this.deviceOsVersion,
            required this.deviceIpAddress,
            required this.lat,
            required this.long,
            required this.buyerGcmid,
            required this.buyerPemid,
            required this.app,
        });
    
        factory SplashPost.fromJson(String str) => SplashPost.fromMap(json.decode(str));
    
        String toJson() => json.encode(toMap());
    
        factory SplashPost.fromMap(Map<String, dynamic> json) => SplashPost(
            deviceType: json["deviceType"],
            deviceId: json["deviceId"],
            deviceName: json["deviceName"],
            deviceOsVersion: json["deviceOSVersion"],
            deviceIpAddress: json["deviceIPAddress"],
            lat: json["lat"]?.toDouble(),
            long: json["long"]?.toDouble(),
            buyerGcmid: json["buyer_gcmid"],
            buyerPemid: json["buyer_pemid"],
            app: App.fromMap(json["app"]),
        );
    
        Map<String, dynamic> toMap() => {
            "deviceType": deviceType,
            "deviceId": deviceId,
            "deviceName": deviceName,
            "deviceOSVersion": deviceOsVersion,
            "deviceIPAddress": deviceIpAddress,
            "lat": lat,
            "long": long,
            "buyer_gcmid": buyerGcmid,
            "buyer_pemid": buyerPemid,
            "app": app.toMap(),
        };
    }
    
    class App {
        String version;
        DateTime installTimeStamp;
        DateTime uninstallTimeStamp;
        DateTime downloadTimeStamp;
    
        App({
            required this.version,
            required this.installTimeStamp,
            required this.uninstallTimeStamp,
            required this.downloadTimeStamp,
        });
    
        factory App.fromJson(String str) => App.fromMap(json.decode(str));
    
        String toJson() => json.encode(toMap());
    
        factory App.fromMap(Map<String, dynamic> json) => App(
            version: json["version"],
            installTimeStamp: DateTime.parse(json["installTimeStamp"]),
            uninstallTimeStamp: DateTime.parse(json["uninstallTimeStamp"]),
            downloadTimeStamp: DateTime.parse(json["downloadTimeStamp"]),
        );
    
        Map<String, dynamic> toMap() => {
            "version": version,
            "installTimeStamp": installTimeStamp.toIso8601String(),
            "uninstallTimeStamp": uninstallTimeStamp.toIso8601String(),
            "downloadTimeStamp": downloadTimeStamp.toIso8601String(),
        };
    }
    
    Login or Signup to reply.
  2. 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']?? {})
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search