我想添加一些数据或字符串到数组列表字符串在我的rest api数据,它不工作。我不知道为什么。这是我的api数据,我从我的api在我的本地服务器的json数据是下面这里。
"hotels": [ {
"image": "http://0.0.0.0:8080/uploads/users/IWpEqUWxUxbWsmIoYfUX/95024057775943965869.jpg",
"bedPrice": "10",
"roomPrice": "30",
"rooms": "50",
"ac": "yes",
"address": "kaambo",
"latitude": "43.0890",
"laundry": "yes",
"name": "jaabir",
"location": "Bosaso",
"details": "Hosbitaalka Sixa Garoowe waxa ka howlgali doona 23/06/2022 Dr. Ahmed Bashe (abu-shafi) (FCPS MRCS-UK) oo ka socda Hosbitalka Shaafi Muqdisho kuna takhasusay qalliimada LAPAROSCOPY-ga sida Xameetida,Uur-kujirta,Eernada,Burooyinka IWM. Dr-ku wuxuu si joogta ah u imanayaa bil kasta insha allah. Wac: 7747373, 5065444, 5065333.Hosbitaalka Sixa Garoowe waxa ka howlgali doona 23/06/2022 Dr. Ahmed Bashe (abu-shafi) (FCPS MRCS-UK) oo ka socda Hosbitalka Shaafi Muqdisho kuna takhasusay qalliimada LAPAROSCOPY-ga sida Xameetida,Uur-kujirta,Eernada,Burooyinka IWM. Dr-ku wuxuu si joogta ah u imanayaa bil kasta insha allah. Wac: 7747373, 5065444, 5065333.Hosbitaalka Sixa Garoowe waxa ka howlgali doona 23/06/2022 Dr. Ahmed Bashe (abu-shafi) (FCPS MRCS-UK) oo ka socda Hosbitalka Shaafi Muqdisho kuna takhasusay qalliimada LAPAROSCOPY-ga sida Xameetida,Uur-kujirta,Eernada,Burooyinka IWM. Dr-ku wuxuu si joogta ah u imanayaa bil kasta insha allah. Wac: 7747373, 5065444, 5065333.",
"id": "456",
"beds": "100",
"internet": "yes",
"fastfood": "yes",
"longitude": "23.0890",
"images": [
"http://0.0.0.0:8080/uploads/users/IWpEqUWxUxbWsmIoYfUX/95024057775943965869.jpg",
"http://0.0.0.0:8080/uploads/users/IWpEqUWxUxbWsmIoYfUX/95024057775943965869.jpg",
"http://0.0.0.0:8080/uploads/users/IWpEqUWxUxbWsmIoYfUX/95024057775943965869.jpg"
],
"likedUsers": [
"HftAroebyiUhaKSmsyrc"
],
"adress": "laanta hawada"
}
]
这是镖码。
_likeUser()async{
String api = "http://0.0.0.0:8080/path=hotels/456";
// this list from previous page _data['likedUsers'].toList();
List<String> likes = widget.likedUsers as List<String>;
if(widget.likedUser.contain(widget.currentUser){
setState((){ _isLiked = false;});
var likeRemoved = likes.remove(widget.currentUser);
await http.patch(Uri.parse(api), body: json.encode(<String, Object>{
"likedUsers": [likeRemoved]
});
}else{
var likeAdded = likes.add(widget.currentUser);
await http.patch(Uri.parse(api), body: json.encode(<String, Object>{
"likedUsers": [likeAdded]
});
setState((){
_isLiked = true;
});
}
}
解决方案在这里
_likeUser()async{
String api = "http://0.0.0.0:8080/path=hotels/456";
// this list from previous page _data['likedUsers'].toList();
var likes = widget.likedUsers;
if(widget.likedUser.contain(widget.currentUser){
// i removed item to list in this here
var remove = likes.remove(widget.currentUser);
setState((){ _isLiked = false;});
await http.patch(Uri.parse(api), body: json.encode(<String, Object>{
"likedUsers": likes,
});
}else{
// i added item to list in this here
var add = likes.add(widget.currentUser);
await http.patch(Uri.parse(api), body: json.encode(<String, Object>{
"likedUsers": likes,
});
setState((){
_isLiked = true;
});
}
}