I am trying to create a dart package/plugin that can be used by an example dart app that runs on Android, iOS, and the web. The problem is that one of the packages that I want to use (I’ll call it package "X") is only compatible with Android and iOS – package X does not support the web platform. If I try to run the application on the web, I get a ton of error messages related to platform compatibility for package X.
Here’s the thing, though. I’ve also written an entirely dart-based library that does 90% of what package X does and can be run on the web. So I have a choice:
- I can simply use MY dart package (and not package X) for all 3 platforms, and all 3 platforms will then have 90% of the desired functionality.
- I can use package X for Android and iOS and MY library for the web. The app running on Android and iOS would have "full functionality" and the app running on the web would have 90% functionality.
I’m trying to figure out how to accomplish option 2 above. I’ve experimented with federated plugins – but those seem to be really about using native code for different platforms – something I’m not interested in doing. I don’t need to invoke code written in some other language. Again, package X is written in dart. As soon as I put package X in the pubspec.yaml
file as a dependency, the deployment on the web goes south. Ideally, there’d be a way to specify different dart dependencies for different platforms.
Is there a way to create a package that loads/imports/uses ONE dart package for one set of platforms (iOS and Android) and, for another platform (web), loads/imports/uses a DIFFERENT dart library?
2
Answers
You can do this with a "monorepo", which is a repo of multiple packages.
Put all your platform-independent logic into a private "common" package (including all your tests for the common logic).
Then create adjacent packages that control what each platform uses from the common package, and the additional specific code for that platform.
These monorepos can be managed rather directly with the "melos" tool, found in the pub.
Take a look at this article:
Conditional Importing – How to compile for all platforms in Flutter