Skip to content

[Flutter 3.9.0+] UI freezes when using FlutterIsolate.spawn (worked fine in 3.7.2) #162

@indo-san

Description

@indo-san

Summary

After upgrading to Flutter 3.9.0 or later, calling FlutterIsolate.spawn(...) causes the UI to freeze during the isolate execution, on both iOS and Android.

This issue does not occur in Flutter 3.7.2 with the same code and flutter_isolate plugin versions.


Environment & Reproduction Matrix

This issue is reproducible on both real devices and simulators (iOS and Android).

Flutter Version flutter_isolate Version Result
3.7.2 2.0.5-pre ✅ OK
3.7.2 2.1.0 ✅ OK
3.9.2 2.0.5-pre ❌ UI freezes
3.9.2 2.1.0 ❌ UI freezes

Reproduction steps

  1. Create a Flutter project and install flutter_isolate.
  2. Use the code below (based on the default Flutter template with minimal changes).
  3. Run the app on a physical device or simulator.
  4. Tap the + button once.
  5. Observe that the UI freezes for 5 seconds during the isolate execution.

Code (Minimal Reproducible Example)

import 'dart:isolate';
import 'package:flutter/material.dart';
import 'package:flutter_isolate/flutter_isolate.dart';

@pragma('vm:entry-point')
Future<void> topLevelFunction(Map<String, dynamic> args) async {
  final sendPort = args['sendPort'] as SendPort;
  final start = DateTime.now();
  while (DateTime.now().difference(start).inSeconds < 5) {
    // Simulate heavy work
  }
  sendPort.send('done');
}

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: const MyHomePage(title: 'Flutter Isolate Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  Future<void> _incrementCounter() async {
    setState(() => _counter++);
    if (_counter == 1) {
      final receivePort = ReceivePort();
      final isolate = await FlutterIsolate.spawn<Map<String, dynamic>>(
        topLevelFunction,
        {'sendPort': receivePort.sendPort},
      );
      await receivePort.first;
      isolate.kill();
      receivePort.close();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('You have pushed the button this many times:'),
            Text('$_counter', style: Theme.of(context).textTheme.headlineMedium),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

Additional Notes

  • The issue seems to have been introduced starting from Flutter 3.9.0.
  • Downgrading to 3.7.2 resolves the issue.
  • Using the standard dart:isolate API (e.g., Isolate.spawn) does not cause any UI freezing. The issue appears to be specific to flutter_isolate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions