“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

Lao Wang’s troubles

With his caution and the help of his beautiful and capable girlfriend Dio, xiao Wang in the past finally made a career and became a famous figure in China. Xiao Wang also became Lao Wang. Now, Lao Wang has been nearly 60 years old, seemingly on the peak of life, but also encountered the troubles of life – that is his son, the new Xiao Wang.

Unlike his father, who had been cautious, Xiao Wang was reluctant to take over Lao Wang’s business after studying abroad. Instead, he became obsessed with the Internet, playing games, spraying people on weibo, and holding up Internet celebrities. The first two items were ok, but the last one really upset Lao Wang. This net red which can casually tease of, in case make a lot of small small wang, how big family is not enough to divide of!

Capable Lao Wang daughter-in-law

At the critical moment, It was Lao Wang’s daughter-in-law. Dio, who had been hiding her mistress in the golden house, came up with a new strategy, which again impressed Lao Wang. Dio, Lao Wang’s daughter-in-law, made an interceptor for Xiao Wang. Whenever Xiao Wang wants to do anything on the Internet, she will intercept it first. Then she will decide whether to send it for him according to what he wants to do. Or when receiving any message, I will read it first and then show it to Xiao Wang. And, most importantly, Wang didn’t know anything about it!

Lao Wang daughter-in-law is so stem at the beginning, xiao Wang has what new trend to report directly to Lao Wang in the Internet.

static Dio getDioInstance() {
  if (_dioInstance == null) {
    _dioInstance = Dio();
    _dioInstance.interceptors
        .add(InterceptorsWrapper(onRequest: (options, handler) {
      print('Report Lao Wang, Xiao Wang has his eye on a new Internet celebrity:' + options.path);
      return handler.next(options);
    }, onResponse: (response, handler) {
      print('Report Lao Wang, Xiao Wang received a reply from the new Internet celebrity:' + response.statusMessage);
      handler.next(response);
    }, onError: (DioError e, handler) {
      print('Report Lao Wang, Xiao Wang was sprayed by net red! ' + e.message);
      return handler.next(e);
    }));
  }

  return _dioInstance;
}
Copy the code

Now Xiao Wang is being watched on the Internet — and he doesn’t even know it! Just, every time he asked for money, Lao Wang no longer casually give it!

But this time, xiao Wang still can be on the net flirt, after all, the Internet in this era is not how to ask for money.

Cruel Old Wang daughter-in-law

When Lao Wang’s daughter-in-law Dio saw that this way was not working, she came up with another plan. Every time Xiao Wang chatted with Internet celebrities, she refused directly and cruenly.

static Dio getDioInstance() {
  if (_dioInstance == null) {
    _dioInstance = Dio();
    _dioInstance.interceptors
        .add(InterceptorsWrapper(onRequest: (options, handler) {
      print('Report Lao Wang, Xiao Wang has his eye on a new Internet celebrity:' + options.path);
      return handler.next(options);
    }, onResponse: (response, handler) {
      print('Report Lao Wang, Xiao Wang received a reply from the new Internet celebrity:' + response.statusMessage);
      throw new Exception('Give up! ');
      //handler.next(response);
    }, onError: (DioError e, handler) {
      print('Report Lao Wang, Xiao Wang was sprayed by net red! ' + e.message);
      return handler.next(e);
    }));
  }

  return _dioInstance;
}
Copy the code

Quiet Xiao Wang

Xiao Wang this down meng circle, is it his those “local flavor love words” has been ineffective? Every time he sent out the news, he was hit with a merciless blow, which made him disheartened. He gradually faded from the Internet, and no one knows what he is doing now. The feeling is like at the beginning Lao Wang gold house hides jiao, now xiao Wang is also gradually hidden. From now on, the Internet leaves the legend of xiao Wang and each net red only.

Afterword.

Through the story of Lao Wang and Xiao Wang, we tell Dio encapsulation and Dio interceptor. Interceptors can be used in many practical scenarios:

  • Permission verification: For example, if the interface requests the backend to return the 401 unauthorized page, the interface can skip to the login page, and the 403 unauthorized page.
  • Exception monitoring: The interceptor can handle the exception and report the exception monitoring background or send the exception warning message;
  • Cache interface: You can cache the requests on some interfaces locally and set a cache validity period. When repeated requests are made within the validity period, data is directly returned to the local cache without requesting the back-end interface, thus reducing the back-end server load. See dio-http-cache for this.
  • Cookies: The App itself does not cache Cookie information. You can use interceptors to automatically carry Cookie information when making requests to the backend. See cookie_manager.
  • Generate an interface document: You can set the request parameters in an interceptor and output the result as an interface document in Postman format. See postman_dio.
  • Custom interceptors: You can customize your own interceptor class, inheritedInterceptorClasses that implementonRequest.onResponseonErrorMethod is ok.

Note that instances of Dio can have multiple interceptors added at the same time to handle different situations.