0

I've created a function that gets a json from an api. The problem is that the function is not returning a value. It returns 'Instance of Future...'

I've tried different ways of solving it. If I print the value that is supposed to return (print(extractdata[1])) It actually returns what it's supposed to:

{
   id: 2
   name: Sala 2,
   beds: [
       {
           id: 3,
           name: Cama 3,
           paciente: null
       },
       {
           id: 4,
           name: Cama 4,
           paciente: null
       }
   ]
}

Future <List> getRoom() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var _token = prefs.getString('token');
    final response = await http.get('http://10.0.2.2:8000/api/room/',
    headers: {'Authorization':'JWT $_token'}
    ).then((res){
      var extractdata = JSON.jsonDecode(res.body);
      return extractdata;
    });
  }

I expect the output to be the list of all rooms but instead it returns 'Instance of Future...'. If I try to get the length it works if I print it inside the function but if I call the function and then ask for length it does not work.

1 Answer 1

1

As you are using async/await, you do not need to use then.

Future <List> getRoom() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var _token = prefs.getString('token');

    final response = await http.get('http://10.0.2.2:8000/api/room/',
        headers: {'Authorization':'JWT $_token'}
    );

    var extractdata = JSON.jsonDecode(response.body);
    return extractdata;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried what you suggest me and it stills retrieve "Receiver: Instance of Future<dynamic>"
Pelase update the question and add how are your code now
Thanks. I added a Future Builder to my code and now its getting the list correctly.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.