简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM