How can I load code from another Dart package, known only at runtime?

dart

Solution

This package https://pub.dartlang.org/packages/plugins seems to do exactly what you want (haven't used it myself yet though) by loading plugins into isolates.

Problem

I am building a Dart application. It needs to load code from a third-party package, which is only known at runtime. My application needs to: - auto-discover the dependency - load a library from that dependency - interact with the dependency Ideally, I do not want to require that my users specify the third-party dependency. The application should auto-discover the dependency. For example, a workflow could be something like this: - User installs my app (`pub global activate my_app`) - User installs a "plugin" (`pub global activate plugin_for_my_app`) - User runs my app (`my_app`) - The app auto-discovers that `plugin_for_my_app` exists. - The app loads the plugin (via `spawnUri` perhaps?) - The app calls into the plugin Requirements: - Must run from the command-line. - Must work on Windows, Mac, Linux. - Should (but doesn't have to) run when compiled to JavaScript. - `pub run` support is optional (`pub run` makes it tricky, because it rewrites your import URIs, so it's not a requirement) What's the best way to do this?

Original source