dio icon indicating copy to clipboard operation
dio copied to clipboard

Issues mixing Dio with BLOC

Open mattRiddoch opened this issue 7 years ago • 2 comments

New Issue Checklist

  • [x] I have searched for a similar issue in the project and found none

Issue Info

Info Value
Platform Name e.g. flutter / ios / android flutter
Platform Version e.g. 1.5.0 / 12.0 / 9.0 1.5.4-hotfix.2
Dio Version e.g. 2.1.0 / 1.0.17 dio: ^2.1.7
Android Studio / Xcode Version e.g. Android Studio 3.3.2 / Xcode 10.2.1 VS Code
Repro rate e.g. all the time (100%) / sometimes x% / only once 100%
Repro with our demo prj e.g. does it happen with our demo project? N/A
Demo project link e.g. link to a demo project that highlights the issue N/A

Issue Description and Steps

Please fill in the detailed description of the issue (full output of any stack trace, compiler error, ...) and the steps to reproduce the issue.

Trying to yield BLOC states from inside an interceptor like so:

request.interceptors.add(InterceptorsWrapper(onRequest: (RequestOptions options) {
          print('RequestOptions $options');
        }, onError: (DioError error) {
          print('error $error');
          yield LoginState.jwtFailed(error.response.statusCode);//FAILING HERE
        }, onResponse: (Response response) {
          print('response: $response');
        }));

I get a 'Error: 'yield' isn't a type.'

I can yield from the catch block like so:

on DioError catch (e) {
        // The request was made and the server responded with a status code
        // that falls out of the range of 2xx and is also not 304.
        if (e.response != null) {
         yield LoginState.jwtFailed(e.response.statusCode);
        } 

Any tips for yielding a BLOC state from inside the interceptors declaration?

mattRiddoch avatar Jul 05 '19 17:07 mattRiddoch

I had a similar requirement, what I did is throw an exception and catch this exception in the bloc itself and then yield proper state based on the caught exception.

Honestly I think APIs/repositories shouldn't be concerned with business logic or yield bloc states as it is the responsibility of the bloc.

amrosama2020 avatar Jul 06 '19 11:07 amrosama2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

stale[bot] avatar Aug 05 '19 11:08 stale[bot]