Isolates and multithreading in Flutter (The Boring Flutter Development Show, Ep. 30)

Flutter · 49K views · 2019 · 59:59

Open on YouTube

Software (other)tutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about Dart isolates, the compute function, and keeping the Flutter UI from lagging during heavy work.

Recommended by 3 people across 3 different months, 2019 to 2023.

What it is

An hour-long episode of the Flutter team's Boring Show in which host Filip and guest Andrew work through isolates and multithreading in Dart, building on the Isolate class docs and a Hacker News app example. It is a live-coding style walkthrough from the official Flutter channel.

What people said

Recommends creativecreatorormaybenot · 2019-11 · Stack Overflow · on “Can I pass a BuildContext to Compute?”

As explained in the documentation [LINK] , no - you cannot send a BuildContext to a compute function, i.e. another Isolate ( compute is only a wrapper for simple isolates). There are limitations on the values that can be sent and received to and from isolates. These limitations constrain the values of Q and R that are possible. See the discussion at SendPort.send [LINK] . The message is the value of Q ( R is the return value) and is therefore subject to the following limitations: The content of message can be: primitive values (null, num, bool, double, String), instances of SendPort [LINK] , and lists and maps whose elements are any of these. List and maps are also allowed to be cyclic. If y

Recommends Parth Dave · 2020-05 · Stack Overflow · on “Is there memory issue with isolate in flutter app?”

For a starter like us, we need to understand that compute function is nothing but the isolate itself. and the more you call to create isolate the more memory you will be needing. This reference Isolates spawn will take ~ 2mb of ram [LINK] and so we need to make isolates as less as possible even though you might say that I'm just computing and returning result so isolate might get GC call but no at a time you might be scrolling and caching or doing something with isolate or your code within that isolate can impact huge in-memory footprint. so rather doing that I suggest you create one isolate and do whatever stuff you want to do and when you finish all you copy faces then close the isolate. w

Recommends Philipos D. · 2021-05 · Stack Overflow · on “Flutter Dart: How can we achieve multithreading like python or java in dart”

You can also check this video from the Flutter team regarding **Isolates and multithreading in Flutter** [this video]

Cites Sourav Ahmed · 2021-04 · Stack Overflow · on “UI lags during heavy computation operation in Flutter even with async”

Instead of this TextButton( child: Text("Hash Password"), onPressed: () { hash("ThisIsTheMasterPassword").then((value) { print(value); }); }) use this TextButton( child: Text("Hash Password"), onPressed: () async{ hash("ThisIsTheMasterPassword").then((value) { print(value); }); }) or TextButton( child: Text("Hash Password"), onPressed: ()async { var value=await hash("ThisIsTheMasterPassword"); print(value); }) You can use isolate to calculate of sth out of the main thread. [this video] compute(hash, "ThisIsTheMasterPassword"); Further reading [LINK]

Cites Yassin Saddem · 2023-05 · Stack Overflow · on “Does Future.wait() run given Futures asynchronous?”

Future.wait([future1(), future2(), future3()]) Run the 3 futures in parallel. It's better than: await future1(); await future2(); await future3(); The following example explain the benefits of Future.wait([]): Future<String> getValue1() { return Future.delayed(Duration(seconds: 2),() => "value1"); } Future<String> getValue() { return Future.delayed(Duration(seconds: 2), () => "value2"); } void main(List<String> arguments) async { var stp = Stopwatch()..start(); await Future.wait([getValue(),getValue1()]); stp.stop(); print("when you execute it with Future.wait: ${stp.elapsedMilliseconds}"); var stp2 = Stopwatch()..start(); await getValue1(); await getValue(); stp2.stop(); print("when you exe