Home > OS >  Flutter : How can i achieve this in flutter?
Flutter : How can i achieve this in flutter?

Time:09-15

[{serialNumber: VNPT29102021, modelName: GW040H, deviceMac: d4:9a:a0:91:55:c8, deviceType: 3, deviceTypeName: ONTMESH, ipAddr: 192.168.1.1, deviceList: [{modelName: EW12ST000T0004, deviceMac: cc:71:90:4a:24:f4, serialNumber: 1291111264A24F2, ipAddr: 192.168.1.3, deviceType: 1}, {modelName: EW12ST000T0004, deviceMac: cc:71:90:4a:25:08, serialNumber: 1291111264A2506, ipAddr: 192.168.1.4, deviceType: 2}], authenString: , dbVersion: 1, cookies: }]

I have a list like above. how can i get "modelName" inside this list?

CodePudding user response:

First of all, This is not a valid dart list. if the list is like the below then, you can access modelName like this:

final list = [{"modelName": "GW040H",}];

final modelName = list[0]["modelName"];

CodePudding user response:

Convert json to model

class Model {
  String? serialNumber;
  String? modelName;
  String? deviceMac;
  int? deviceType;
  String? deviceTypeName;
  String? ipAddr;
  List<DeviceList>? deviceList;
  String? authenString;
  int? dbVersion;
  String? cookies;

  Model(
      {this.serialNumber,
        this.modelName,
        this.deviceMac,
        this.deviceType,
        this.deviceTypeName,
        this.ipAddr,
        this.deviceList,
        this.authenString,
        this.dbVersion,
        this.cookies});

  Model.fromJson(Map<String, dynamic> json) {
    serialNumber = json['serialNumber'];
    modelName = json['modelName'];
    deviceMac = json['deviceMac'];
    deviceType = json['deviceType'];
    deviceTypeName = json['deviceTypeName'];
    ipAddr = json['ipAddr'];
    if (json['deviceList'] != null) {
      deviceList = <DeviceList>[];
      json['deviceList'].forEach((v) {
        deviceList!.add(DeviceList.fromJson(v));
      });
    }
    authenString = json['authenString'];
    dbVersion = json['dbVersion'];
    cookies = json['cookies'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['serialNumber'] = serialNumber;
    data['modelName'] = modelName;
    data['deviceMac'] = deviceMac;
    data['deviceType'] = deviceType;
    data['deviceTypeName'] = deviceTypeName;
    data['ipAddr'] = ipAddr;
    if (deviceList != null) {
      data['deviceList'] = deviceList!.map((v) => v.toJson()).toList();
    }
    data['authenString'] = authenString;
    data['dbVersion'] = dbVersion;
    data['cookies'] = cookies;
    return data;
  }
}

class DeviceList {
  String? modelName;
  String? deviceMac;
  String? serialNumber;
  String? ipAddr;
  int? deviceType;

  DeviceList(
      {this.modelName,
        this.deviceMac,
        this.serialNumber,
        this.ipAddr,
        this.deviceType});

  DeviceList.fromJson(Map<String, dynamic> json) {
    modelName = json['modelName'];
    deviceMac = json['deviceMac'];
    serialNumber = json['serialNumber'];
    ipAddr = json['ipAddr'];
    deviceType = json['deviceType'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['modelName'] = modelName;
    data['deviceMac'] = deviceMac;
    data['serialNumber'] = serialNumber;
    data['ipAddr'] = ipAddr;
    data['deviceType'] = deviceType;
    return data;
  }
}

And you can get modelName in deviceList:

Model.deviceList[0].modelName
  • Related