-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Need a way to pass messages back to the native app code #10384
Description
This is meant to be a discussion thread, we are already implementing a solution for this. We wanted to document the problem and potential solutions before we finalize it.
Leafy is writing a Flutter plugin to do logging specific to Google infrastructure. This plugin, unfortunately, needs authentication to work on iOS. The problem is that authentication token/object comes from a different plugin.
Normally, in native apps, this is solved by having the app code manage the flow between these two services (auth and log) once authentication is done. This can be done in Flutter too if it is managed entirely in native code. However, the proper way to do authentication is via plugins. Dart app code orchestrates the auth flow. That means I need to be able to somehow signal the native app code that the auth is done. So far, we thought of several solutions; none of which is perfect:
-
Create a dependency between the log and the auth plugin. Once auth is done, auth notifies the log plugin. This does not work because log plugin is Google specific while auth can be OSS.
-
Make auth plugin publish an event such as "authComplete" and have main app code subscribe to it. This would work but does not reflect the reality where authentication is actually orchestrated on Dart side; not native. It also feels like a one-off. What about errors? Do we also publish "authError"?
-
Pass a message from Dart to native app code directly. This mechanism does not exist currently and could be tricky to implement (e.g. we need this to work on any Android activity, which is the direction Flutter wants to go). This solution is interesting because we do have communication of lifecycle events from native -> dart at the moment. So it would be good to have a symmetry where dart can define its own lifecycle messages and deliver to native side.
-
Implement a callback plugin that can be configured by the native app code to run a function when a signal is received from Dart side. However, Flutter is moving towards a easy-to-use plugin model where main app code does not even get an instance of the plugin. Registration is done automatically.
We are currently leaning toward implementing the last solution. The idea is to do something like this:
Native app code:
onCreate() {
AuthPlugin auth = new AuthPlugin();
LogPlugin log = new LogPlugin();
CallbackPlugin.setupCallback("authComplete", () -> {
log.configureLog(auth);
});
}Dart code:
// When auth is complete
callbackPlugin.runCallback('authComplete');Please comment on whether this is a workable solution. Also, please let me know if it is possible to get a hold of automatically registered plugin instances in native app code. If not, callback plugin approach would not work.