-
Notifications
You must be signed in to change notification settings - Fork 81
Open
Description
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
- Create a Flutter project and install
flutter_isolate. - Use the code below (based on the default Flutter template with minimal changes).
- Run the app on a physical device or simulator.
- Tap the
+button once. - 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:isolateAPI (e.g.,Isolate.spawn) does not cause any UI freezing. The issue appears to be specific toflutter_isolate.
Metadata
Metadata
Assignees
Labels
No labels