Flutter WidgetsFlutterBinding.ensureInitialized() appears when dealing with rootBundle inside an isolate

Viewed 1559

In my Flutter app, there is a heavy task that forces me to use isolates in order to have a smooth UI, and that task includes reading some assets files using the rootBundle and searching through them for a certain text. Now since the main isolate that renders the UI doesn't share memory with the spawned isolate that will do the heavy task for me, whenever I try to use the rootBundle inside the method which the spawned isolate runs, I get this error:

ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.

And of course using WidgetsFlutterBinding.ensureInitialized(); at the top of the method doesn't work. And I tried to send the rootBundle to the spawned Isolate and using it there but that didn't work.

1 Answers

I reported this to flutter devs: https://github.com/flutter/flutter/issues/61480

They responded saying that this is not a bug, maybe it is as-is. Checking the flutters samples you can find this:

// Load the JSON string. This is done in the main isolate because spawned
// isolates do not have access to the root bundle. However, the loading
// process is asynchronous, so the UI will not block while the file is
// loaded.

The full example file:

Blockquote

Related