Flutter & Dart (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What are Dart Isolates and how do you achieve true multithreading?
senior LevelCode ExampleSystem Design
In Simple Words: Because Dart is single-threaded, a heavy calculation (e.g. parsing a 20 MB JSON payload) freezes the UI frame rate:
- Isolate: An independent execution worker with its own isolated memory heap and event loop.
- Isolates share zero memory with each other! Data is passed strictly via message passing ports (
ReceivePort,SendPort). Isolate.run()/compute(): Spawns an isolate, executes a heavy function on another CPU core, and returns the result asynchronously without dropping a single UI frame.
dart
// Run heavy computation in background Isolate:
List<User> parseLargeJson(String jsonString) { ... }
Future<List<User>> loadUsers(String rawData) async {
return await Isolate.run(() => parseLargeJson(rawData));
}