I want to verify that long running Isolates would cause ANR(App not responding) in Flutter. For that I bootstrapped flutter code and created a button which when pressed would execute a very long running operation(on different isolate) that I am expecting it would block Main Isolate causing App to be unresponsive. I saw that Future.delayed uses Timer internally so both Timer and Future are running on different isolate but the app is still fully responsive.
Expected Behavior: Long running Isolates launched on button tap should stop Main Isolate(which is updating UI) execution. Observed Behavior: UI is fuly repsonsive even though there are other long running isolates.
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
ElevatedButton(
child: const Text("Press me to Trigger ANR"),
onPressed: () {
Timer(const Duration(seconds: 10), () {
debugPrint('Now 10 seconds ANR is over.');
}); //---------------------> This is not causing ANR
//Future.delayed(const Duration(seconds: 30), () {
// debugPrint('Now 30 seconds ANR is over.');
//}); ---------------------> This also does not cause ANR
},
),
],
),
To trigger an App Not Responding (ANR) error in Flutter, you need to block the main isolate's event loop. Using Timer or Future.delayed won't cause an ANR because they run asynchronously and do not block the main isolate.
Here's how you can simulate a long-running operation on the main isolate that would block it and cause an ANR: