0

I'm working on an image processing app. I have an animated gif to show progress. As Dart is a single thread, every time the image is being processed, it locks UI, including the animated gif. I don't want to use isolates to avoid potential memory leaks. I want to implement something using event loop, similar to the link below.

https://hackernoon.com/executing-heavy-tasks-without-blocking-the-main-thread-on-flutter-6mx31lh

I have tried to insert some delay during pixel looping as suggested in this thread but it doesn't work.

Dart future blocking main thread

Future<image> process2() {
  return Future(() async {
    for (var x = 0; x < width; x++) {
      for (var y = 0; y < height; y++) {        
        // process       
      }

      if (x % 100 == 0) {
        await Future.delayed(Duration(seconds: 100));
      }
    }
  });
}
jdevp2
  • 371
  • 5
  • 15
  • Your best bet is probably to use Isolate.spawn to create a new thread. Recent advances in the Dart VM have made Isolates have very little overhead getting data to and from the main Isolate. – Randal Schwartz Oct 05 '22 at 18:38

0 Answers0