Flutter cancel task

foreword

During the development process, canceling requirements is very common, but easily overlooked. However, the benefits of removing the requirement are also great. For example, many requests are sent in the page. If the page is cut away and is in an invisible state, it is necessary to cancel the unfinished request task. If not canceled in time, it may result in the following negative effects:

  1. Consume additional user data traffic.
  2. The task callback holds the global context variable, and there is a risk of memory leak if it is not released in time
  3. Excessive asynchronous requests consume a lot of system resources, slow down the UI thread, and cause lag.

In Flutter, how to cancel tasks that are already in progress? First, you need to master some basic knowledge.

Pre-knowledge

Future#any method

Pass in a list of Future tasks and return the first completed asynchronous task, whether successful or unsuccessful.

definition

usage

For the following 5 asynchronous tasks, although the fifth line of code is executed second, it is executed first, so the first one returns, so far the entire Future.any function has been executed.

The resulting input is as follows:

Summarize

  • Future.any is actually a Completer, where N Futures are stacked together, whoever completes first counts.
  • Future.any can be understood as a 100-meter race, in which each player is a Future, whoever runs the fastest to the finish line wins.

Dio cancel implementation parsing

dio version

dio: dev v5.0.3

git: 67f07b86a0976c14a6e19061563832d92ed6772b

branch: main

how to cancel

Pass in the CancelToken object in the request, and then call the token.cancel method

final cancelToken = CancelToken();
dio.get(url, cancelToken: cancelToken).catchError((DioError err) {
  if (CancelToken.isCancel(err)) {
    print('Request canceled: ${err.message}');
  } else{
    // handle error.
  }
});
// Cancel the requests with "cancelled" message.
token.cancel('cancelled');


Process Analysis

Idea: Use the Future.any function to insert and cancel the task before the actual task is executed. If the actual task is not finished, there is an opportunity to cancel it.

The code is as follows, the yellow mark can be read according to the steps.

If you lack direction on the way to advancement, you can scan the QR code below to join our circle and learn and communicate with Android developers!

All the following content can be obtained by scanning the QR code below!

  • A full set of manuals for Android advanced learning

    img

  • Android Benchmark Ali P7 Learning Video

    img

  • BATJ big factory Android high-frequency interview questions

    img

Finally, I end this article with one of my favorite quotes from Jobs:

People can't do too many things in this life, so everything must be done wonderfully.
Your time is limited, so don't live it for others. Don't be limited by dogma, don't live in other people's ideas. Don't let other people's opinions dictate your own inner voice.
Most importantly, be brave enough to follow your heart and intuition. Only your heart and intuition know what you really think, and everything else is secondary.

Guess you like

Origin blog.csdn.net/Eqiqi/article/details/130071380