Sub topic 13
Platform limits and the web
Assume you know basic Dart syntax, async and await, Future, and simple streams. The earlier chapters introduced isolates as separate Dart heaps that communicate through messages. This model works well on native platforms, but it does not apply equally to every Flutter target.
Native platforms and the web
On Android, iOS, Windows, macOS, and Linux, Dart can create additional isolates. A worker isolate runs Dart code separately from the isolate that owns your Flutter UI. A long calculation in that worker does not block UI event handling.
Dart on the web has a different runtime model. The web target does not provide the same native isolate support. Flutter's compute function still exists, but on the web it runs the callback on the current event loop instead of moving it to a separate Dart isolate. The calculation can therefore still block rendering.
| Target | `Isolate.run` and `Isolate.spawn` | Flutter `compute` | Main consequence |
|---|---|---|---|
| Native Flutter | Use separate isolates | Uses a worker isolate | Heavy Dart work can leave the UI isolate responsive |
| Flutter web | No native isolate model | Runs on the current event loop | Heavy work can still cause jank |
| Web with a manual web worker | Requires web-specific setup | Does not create it automatically | You must define worker communication yourself |
A web worker written with JavaScript APIs can run code outside the browser's main thread. This is a separate solution from Dart isolates. It may require JavaScript interoperability, custom worker files, and explicit message serialization. Use it only when web performance requirements justify the extra platform-specific code.
Choose a web fallback
For native builds, compute is often a simple choice for one-off work. For the web, divide a large job into smaller pieces and yield to the event loop between pieces. Yielding gives the browser a chance to process input, layout, and painting.
import 'package:flutter/foundation.dart';
int sumChunk(List<int> values) {
var total = 0;
for (final value in values) {
total += value;
}
return total;
}
Future<int> sumInChunks(List<int> values) async {
var total = 0;
for (var start = 0; start < values.length; start += 10000) {
final end = (start + 10000 < values.length)
? start + 10000
: values.length;
total += sumChunk(values.sublist(start, end));
// Let the browser handle pending UI work.
await Future<void>.delayed(Duration.zero);
}
return total;
}
Future<int> sumValues(List<int> values) {
if (kIsWeb) {
return sumInChunks(values);
}
return compute(sumChunk, values);
}
Future<void> main() async {
final values = List<int>.generate(100000, (index) => index);
final result = await sumValues(values);
debugPrint('Total: $result');
}Chunking does not make the calculation run in parallel. It limits how long each uninterrupted section runs. Smaller chunks improve responsiveness but add scheduling overhead. Larger chunks finish sooner but can still produce visible pauses.
Platform plugins have their own limits
An isolate cannot freely call Flutter plugins. On native platforms, a background isolate must initialize the binary messenger with the root isolate's token, and the specific plugin must support background-isolate calls. Many plugins require access to the main isolate, a view, or a platform lifecycle, so initialization alone is not enough.
On the web, plugins use web implementations. A background Dart isolate is not available to provide a general replacement for the browser's main thread. Check each plugin's platform documentation before moving plugin calls into a worker design.
Make the decision
Use an isolate when the target supports it and the job is large enough to repay the cost of starting an isolate and copying messages. For small calculations, normal asynchronous code is simpler. For web builds, measure before choosing a manual worker. Often, pagination, incremental parsing, server-side processing, or chunking solves the problem with less platform-specific code.
Exercise
Why does replacing compute(expensiveFunction, data) with Future(() => expensiveFunction(data)) fail to solve jank in a Flutter web build?
Show answer
Both forms run the expensive synchronous function on the browser's current event loop. Future changes scheduling, not the execution thread. Use chunking with event-loop yields or a separately configured web worker.