Issue
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.
Solution
You simply change to
ResultData outputResult = jsonDecode(sendSearch.body);
print(outputResult);
List<Result> resData =outputResult.result;
print(resData.first);
Answered By - SAT TechnoLab
Answer Checked By - Cary Denson (JavaFixing Admin)