Thanks for open my question, i'll post my code first, then i'll put the question below it.
ResultData class
ResultData resultDataFromMap(String str) =>
ResultData.fromMap(json.decode(str));
String resultDataToMap(ResultData data) => json.encode(data.toMap());
class ResultData {
ResultData({
required this.trxid,
required this.datetime,
required this.reqid,
required this.id,
required this.responsecode,
required this.message,
required this.serverkey,
required this.result,
});
String trxid;
String datetime;
String reqid;
String id;
String responsecode;
String message;
int serverkey;
List<Result> result;
factory ResultData.fromMap(Map<String, dynamic> json) => ResultData(
trxid: json["trxid"],
datetime: json["datetime"],
reqid: json["reqid"],
id: json["id"],
responsecode: json["responsecode"],
message: json["message"],
serverkey: json["serverkey"],
result: List<Result>.from(json["result"].map((x) => Result.fromMap(x))),
);
Map<String, dynamic> toMap() => {
"trxid": trxid,
"datetime": datetime,
"reqid": reqid,
"id": id,
"responsecode": responsecode,
"message": message,
"serverkey": serverkey,
"result": List<dynamic>.from(result.map((x) => x.toMap())),
};
}
class Result {
Result({
required this.stockid,
required this.itemname,
required this.unit,
required this.costperunit,
});
String stockid;
String itemname;
String unit;
String costperunit;
factory Result.fromMap(Map<String, dynamic> json) => Result(
stockid: json["stockid"],
itemname: json["itemname"],
unit: json["unit"],
costperunit: json["costperunit"],
);
Map<String, dynamic> toMap() => {
"stockid": stockid,
"itemname": itemname,
"unit": unit,
"costperunit": costperunit,
};
}
searchRequest
static searchRequest(String searchVal, hasilSearch) async {
var searchValue = searchVal;
try {
var sendSearch = await http.post(Uri.https('www.domain.net', '/ptemp/'),
headers: {'x-v2rp-key': '$conve'},
body: jsonEncode({
"trxid": "$trxid",
"datetime": "$datetime",
"reqid": "0002",
"id": "$searchValue"
}));
var outputResult = jsonDecode(sendSearch.body)['result'] as List;
print(outputResult);
List<Result> resData =
outputResult.map((json) => Result.fromMap(json)).toList();
print(resData.first);
} catch (e) {
print(e);
}
}
}
Response i get from API :
{
"trxid":"1656385782731",
"datetime":"20220628100942",
"reqid":"0002",
"id":"sepatu",
"responsecode":"00",
"message":"Success",
"serverkey":1656385795752,
"result":[
{
"stockid":"COVERSEPATU001",
"itemname":"Cover Sepatu Plastik APD",
"unit":"piece","costperunit":"0"
},
{
"stockid":"DIVINGBREATH001SP001",
"itemname":"Fin\/ Sepatu Selam Kaki Katak",
"unit":"piece",
"costperunit":"0"
},
{
"stockid":"DIVINGBREATH001SP009",
"itemname":"Sepatu Selam",
"unit":"piece",
"costperunit":"0"
},
{
"stockid":"RAKSEPATU001",
"itemname":"Rak Sepatu 4 Tingkat",
"unit":"piece",
"costperunit":"166309.52"
}
]
}
The Question is = how can i store the response data into Resultdata and result class?, and how can i display them using futurebuilder listview?
i really stuck here for 5 days, been googling and still can't solve this.
CodePudding user response:
so those data classes use from json to covert to class and from json to convert to json:
Welcome welcomeFromJson(String str) => Welcome.fromJson(json.decode(str));
String welcomeToJson(Welcome data) => json.encode(data.toJson());
class Welcome {
Welcome({
this.trxid,
this.datetime,
this.reqid,
this.id,
this.responsecode,
this.message,
this.serverkey,
this.result,
});
String trxid;
String datetime;
String reqid;
String id;
String responsecode;
String message;
int serverkey;
List<Result> result;
factory Welcome.fromJson(Map<String, dynamic> json) => Welcome(
trxid: json["trxid"],
datetime: json["datetime"],
reqid: json["reqid"],
id: json["id"],
responsecode: json["responsecode"],
message: json["message"],
serverkey: json["serverkey"],
result: List<Result>.from(json["result"].map((x) => Result.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"trxid": trxid,
"datetime": datetime,
"reqid": reqid,
"id": id,
"responsecode": responsecode,
"message": message,
"serverkey": serverkey,
"result": List<dynamic>.from(result.map((x) => x.toJson())),
};
}
class Result {
Result({
this.stockid,
this.itemname,
this.unit,
this.costperunit,
});
String stockid;
String itemname;
String unit;
String costperunit;
factory Result.fromJson(Map<String, dynamic> json) => Result(
stockid: json["stockid"],
itemname: json["itemname"],
unit: json["unit"],
costperunit: json["costperunit"],
);
Map<String, dynamic> toJson() => {
"stockid": stockid,
"itemname": itemname,
"unit": unit,
"costperunit": costperunit,
};
}
CodePudding user response:
Use json to dart to create your model
Your model:
class Model {
String? trxid;
String? datetime;
String? reqid;
String? id;
String? responsecode;
String? message;
int? serverkey;
List<Result>? result;
Model(
{this.trxid,
this.datetime,
this.reqid,
this.id,
this.responsecode,
this.message,
this.serverkey,
this.result});
Model.fromJson(Map<String, dynamic> json) {
trxid = json['trxid'];
datetime = json['datetime'];
reqid = json['reqid'];
id = json['id'];
responsecode = json['responsecode'];
message = json['message'];
serverkey = json['serverkey'];
if (json['result'] != null) {
result = <Result>[];
json['result'].forEach((v) {
result!.add(new Result.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['trxid'] = this.trxid;
data['datetime'] = this.datetime;
data['reqid'] = this.reqid;
data['id'] = this.id;
data['responsecode'] = this.responsecode;
data['message'] = this.message;
data['serverkey'] = this.serverkey;
if (this.result != null) {
data['result'] = this.result!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Result {
String? stockid;
String? itemname;
String? unit;
String? costperunit;
Result({this.stockid, this.itemname, this.unit, this.costperunit});
Result.fromJson(Map<String, dynamic> json) {
stockid = json['stockid'];
itemname = json['itemname'];
unit = json['unit'];
costperunit = json['costperunit'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['stockid'] = this.stockid;
data['itemname'] = this.itemname;
data['unit'] = this.unit;
data['costperunit'] = this.costperunit;
return data;
}
}
And store your data: var Model data = Model.fromJson(response data);
Refer FutureBuilder
widget here
CodePudding user response:
You simply change to
ResultData outputResult = jsonDecode(sendSearch.body);
print(outputResult);
List<Result> resData =outputResult.result;
print(resData.first);