-1

I wanna add some data or string to an array list strings in my rest api data, an it's not working. I don't know why. Here is my api data which i got from my api in my local server the json data is below here.

 "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"
        }
]

And here is dart code.

_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;
        });
       }
     }
2
  • "likedUsers": [likeRemoved] , it's expects array, your likeRemoved itself is array just pass it. "likedUsers": likeRemoved
    – shorol
    Commented Jun 18, 2022 at 19:30
  • If i do so it's showing an error , likeRemoved is a void . which i did not understand what is meant. Commented Jun 18, 2022 at 19:42

1 Answer 1

0

The solution is here

_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;
            });
           }
         }
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jun 22, 2022 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.