Home > Back-end >  flutter print a single value from json api
flutter print a single value from json api

Time:03-29

i am new to flutter and want to print only my name my code is below i tryed but i am getting error

var resBody = await http.post(
        my_url,
        headers: header,
      );

      print(resBody.body);

i get data like this

[{name:hari,age:26,sex:male}]

i want to print name only i tryed like this but it does not work i dont know whats wrong here

print(resBody.body.name);

i getiing error how do i print any data like that

CodePudding user response:

You can access the data using key of the json object try this

 var resBody = await http.post(
                my_url,
                headers: header,
              );
        
             final data=jsonDecode(resBody.body)//Parses the string and returns the resulting Json object
        
        print(data[0]["name"]

CodePudding user response:

Try below code,and used jsonDecode(resBody.body)['name'] jsonDecode

var resBody = await http.post(
        my_url,
        headers: header,
      );
final jsonData=jsonDecode(resBody.body)['name'];//it prints only name of your json string

      print(jsonData);
  • Related