Global dependency on flutter_bloc Global dependency on flutter_bloc dart dart

Global dependency on flutter_bloc


All the Repositories need a token to make the API requests. The firstproblem came here. How can I get the token from all the repositories?If I use a UserRepository to store the token, I will need to pass itas dependency to each Repository (probably works but I don't think itsthe right way to do it). So, what can be the right way to manage this?

This is not a bad practice to apply (adding it as dependency), but to make it easier, you can use getIt and injectable libraries to have a proper dependency injection solution.

Another way is, adding an interceptor to your networking layer, which is, before making a http request you can intercept your request, add token to your request in that layer and proceed to requesting phase.

Most of the networking libraries has request and response interceptors like below:

But this is not an issue of Bloc library.

If somehow I can get the token on all the repositories queries, whathappens when the token is revoked?

You can add another response interceptor to detect if you have unauthenticated response so you can logout user.

Using getIt, you can gether that bloc from your global dependency container and logout user.