You can accomplish this with conditional imports. This answer provides an excellent method of doing this. The following are the essentials of that post:
The core idea is as follows.
- Create an abstract class to define the methods you will need to use in general.
- Create implementations specific to
web
and android
dependencies which extends this abstract class.
- Create a stub which exposes a method to return the instance of this abstract implementation. This is only to keep the dart analysis tool happy.
- In the abstract class import this stub file along with the conditional imports specific for
mobile
and web
. Then in its factory constructor return the instance of the specific implementation. This will be handled automatically by conditional import if written correctly.
This method allows for you to do these imports based on platform and applies to all packages that may not support every possible flutter platform(e.g. dart:html, dart:js, dart:js_util, dart:io). It seems like the best way of handling different platforms with the same codebase at the moment.
As far as I know, you can't conditionally exclude plugins from pubspec.yaml
(I may of course be wrong), though this shouldn't be necessary with the conditional imports that I mentioned earlier on.
Any native Android or iOS code that's a part of plugins you use is simply not included when you build for web. It's exactly the same for Android and iOS individually. When building for Android, iOS code simply isn't considered when the app is built. Building a flutter app only compiles dart code. It doesn't do anything special with native code other than what building a native app would have done.