0

I'm using Dio for http requests and the function for post method is like this :

  Future<Response?> post(String url, dynamic data) async {
    try {
      Response response = await baseAPI.post(url, data: data);
      return response;
    } on DioError catch(e) {
      throw Failure(e.message);
    }
  }

then when I use this post method the response I get is in Instance of 'Future<Response?>'. So how can I access the response data inside this?

void login(String email, String password) {
    dynamic data = jsonEncode(<String, String>{
      'email': email,
      'password':password,
    });

    Future<Response?> response = loginService.post('https://reqres.in/api/login',data) ;
    print(response);
    print('response data print');
   
    }
Shashiwadana
  • 492
  • 1
  • 8
  • 16

1 Answers1

0

as your loginService.post is returning a future type, you can get the Response value by adding await in front of it, but then your login function will have be declare it as async, such as:

Future<void> login(String email, String password) async {
dynamic data = jsonEncode(<String, String>{
  'email': email,
  'password':password,
});

Response? response = await loginService.post('https://reqres.in/api/login',data) ;
print(response);
print('response data print');

}

Or if you do not wish to async your login function, you can add .then to your post loginService.post like below:

Response? response;
loginService.post('https://reqres.in/api/login',data).then((data) => response = data)
nickypangers
  • 116
  • 6