Unable to call a platform channel method from another isolate
Problem
Status 9/9/22: Work- in-progress Description When I try to invoke a platform channel method from a custom spawned isolate, the app crashes badly (both on iOS and Android). I'm trying to figure out whether this is expected or not. If it's not, it's probably worth to mention that somewhere. Anyways I think that this can potentially be a strong limitation. Is there a way to be able to call platform plugins from a secondary isolate? PRs - [x] iOS Engine - https://github.com/flutter/engine/pull/35174 - [x] iOS Framework - https://github.com/flutter/flutter/pull/109005 - [x] Reland iOS Framework - https://github.com/flutter/flutter/pull/111320 - [x] Android Engine - https://github.com/flutter/engine/pull/35804 - [x] Android Framework - https://github.com/flutter/flutter/pull/111279 - [x] Add macOS platform channel integration tests - https://github.com/flutter/flutter/pull/110606 - [x] Desktop Engine - https://github.com/flutter/engine/pull/35893 - [x] Desktop Framework - https://github.com/flutter/flutter/pull/110882 - [ ] Add samplecode - https://github.com/flutter/flutter/issues/112235 - [ ] Update website documentation - https://github.com/flutter/website/pull/7592 - [x] Make it work with plugins that require a dart plugin registrant - https://github.com/flutter/flutter/issues/112240
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: Unable to call a platform channel method from another isolate
I have various use cases, e.g.: - Download and parse a huge chunk of data (dart:convert methods are not async) , then store it to a sqlite db using sqflite plugin (which uses platform bindings); - Prefetch and parse data before serving it to the ui; - Encrypt/decrypt and read/write data from/to a file (crypto stuff is done through a method channel in order to use platform security libs); In gener
Trust Score
7 verifications
- 1
I have various use cases, e.g.:
- Download and parse a huge chunk of data (dart:convert methods are not async) , then store it to a sqlite db using sqflite plugin (which uses platform bindings); - Prefetch and parse data before serving it to the ui; - Encrypt/decrypt and read/write data from/to a file (crypto stuff is done through a method channel in order to use platform security libs);
- 2
In general it can happen that you use dependencies that declare some public meth
In general it can happen that you use dependencies that declare some public methods which you don't really know if eventually will use platform-specific code to accomplish their stuff (I'm thinking about flutterfire for example); using them imho should be more an implementation detail that can change with time instead of something written in stone.
- 3
Your workaround seems ok for the moment and relatively painless to implement, as
Your workaround seems ok for the moment and relatively painless to implement, as data is already encoded in a way to be passed to method channel, and for this reason will pass easily also via an isolate port, however my guess is that performances would be suboptimal.
- 4
However I got a bit stuck during implementation: can you provide an example of a
However I got a bit stuck during implementation: can you provide an example of an easy way to invoke a static method on the main isolate from a secondary one?
Validation
Resolved in flutter/flutter GitHub issue #13937. Community reactions: 39 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep