Search code examples
flutterhttpjwtdio

How to handle multiple API requests when the auth token is expired?


I have a use case wherein I am calling 2 different APIs at almost the same time, but while executing the 1st API it turns out that my auth token is expired. Now I can pause the execution of API 1 while I get a new token, I want the execution of the 2nd API to pause too, and on getting the valid token, it should resume.


Solution

  • You can do this with dio. You can lock particular Object by lock(). If lock() is called all the further request will be placed in a queue, the calls will be made after when the unlock() is called.

    Dio _dio= Dio();
    
    _dio.lock();
    
    _dio.unlock();