-1

I am trying to convert the response from api to string in flutter. I am still a beginner to Flutter so I'm sorry about that. Whenever I do the get and assign it to a variable, the value is always 'Instance of Future' rather than the actual text? So does this mean I have to convert it to text? This is the code that I used.

gethttps() async{
  var response = await http.get(url);

  print("JSON" + response.body);

  String data = response.body;
  data = json.decode(response.body.toString());
  return data;
}

CALL FROM OUTSIDE THE FUNCTION

var response = gethttps();

CSXZ
  • 11
  • 2

2 Answers2

1

you can follow the link : https://stackoverflow.com/a/50296350/6413387

Basically you have two options

gethttps().then((result) {
  print(result);
  setState(() {
    someVal = result;
  })
})

or

funcThatMakesAsyncCall() async {
  String result = await gethttps();
  print(result);  
  setState(() {
    someVal = result;
  })
}
towhid
  • 2,778
  • 5
  • 18
  • 28
0

You can change signature of function to this one

Future<String> gethttps() async{....}

and then use

var data=await gethttps();

user await

Shojaeddin
  • 1,851
  • 1
  • 18
  • 16