Home > Net >  Can't get values from api as double or int
Can't get values from api as double or int

Time:06-24

I'm facing this problem where I can't return a value from my API as a double.

 Future<int> getStock(String refProd) async {
int stok = 0;
final response = await http.get(
  Uri.parse("http://127.0.0.1:8000/api/produit/stock/$refProd"),
  headers: <String, String>{
    'Content-Type': 'application/json; charset=utf-8',
    'Accept': 'application/json; charset=utf-8',
  },
);
if (response.statusCode == 200) {
  setState(() {
    stok = jsonDecode(response.body);
  });
  return stok;
} else {
  throw Exception('DB ERROR!');
}

}

Trying to print it like this :

print(getstock('1200').toString());

It returns : InstanceofFuture<int> but not the returned value which is stok .

CodePudding user response:

try this :

int stock= await getStock('1200');

or directly

print(await getstock('1200').toString());

CodePudding user response:

Your function is async and therefore returns a Future. You could await the result of getStock before using it, like so:

print(await getstock('1200').toString());

  • Related