Explanation and difference between Completer and Isolates in Flutter

Issue

I needed to return a Future on my app because i thought my function would be taking too much time to complete.

So i got two possible answers: Use a Completer or an Isolate.

What would make me choose one or the other?
Can i put a Completer anywhere i want and trust this will work fine?

And how does this work? Like.. Flutter must draw on screen in 60fps right? So, it will compute this stuff after drawing on the canvas and before the next drawing? I am curious because Flutter is not multithreaded (but appears that isolates can do this).. So i am very confused and don`t know how to write trustworthy code.

And also, is there a reason to not use a Completer? Like, i think my code is taking too much time, but maybe it is not.. Is there a reason to check the time it takes to complete and if it is below a threshold i should not use it?

Thanks in advance.

Solution

This article helped me a lot to understand this assyncronous stuff: https://www.didierboelens.com/2019/01/futures—isolates—event-loop/

Besides explaining it with code, i think the conclusion answers my question:

Therefore, here are some hints you should systematically take into consideration in your developments:

If pieces of codes MAY NOT be interrupted, use a normal synchronous process (one method or multiple methods that call each other);

If pieces of codes could run independently WITHOUT impacting the fluidity of the application, consider using the Event Loop via the use of Futures;

If heavy processing might take some time to complete and could potentially impact the fluidity of the application, consider using Isolates.

Answered By – Daniel Vilela

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *