Heaven Aldrico
Heaven Aldrico

Reputation: 67

Dio not sending request and stuck on waiting the request

I created a class called "Service" that has a Dio instance called http..

Class Service {
  Dio http = Dio(
    BaseOptions(
      baseUrl: 'http://10.0.2.2/api',
    ),
  );
}

And then I created a subclass of the Service class called AuthenticationService which I serve it with Provider so I can access it anywhere. Then I called the function from the subclass like this

Class AuthenticationService extends Service {
  Future<String?> SignIn({String? email, String? Password}) async {
    Response? response;

    try {
      response = await http.post('/auth/login', data: { 'email': email, 'password': password });
    } catch(e) {
      print(e);
    }

    return response?.data.toString(); //token
  }
}

Then I called it from my widget

print('Passed!') // Passed

await context.read<AuthenticationService>().signIn(
  email: _emailController.text,
  password: _passwordController.text,
);

print('Passed?') // Never printed

Is there anything I miss? any suggestion or solution will help.. thank you
(PS: I did check my backend and it also never made it there)

Upvotes: 4

Views: 1291

Answers (1)

DevQt
DevQt

Reputation: 719

I simplified the process for the purpose of simplicity to focus only on the solution:

final response = await dio.post(
  url.toString(),
  data: { 'email': email, 'password': password },
  options: Options(
      responseType: ResponseType.json,
      receiveTimeout: Duration(minutes: 5)), // [REQUIRED] It's up to you what duration you prefer here
);

The [REQUIRED] portion is to address the issue you are encountering.

I observed that it is because of your API's process to prepare the data. It should have enough time to process your request. Therefore, you need to indicate the receiveTimeout.

EDIT: Recommendations

Please strictly follow the documentation about Performing a POST request properly

I hope it helps!

Upvotes: 0

Related Questions