It’s difficult to resist the urge to bang your head on the desk when you encounter an error like this. This is how Flutter reports a dependency conflict. A dependency conflict, while frustrating, is nothing uncommon. Even more so when using a cross-platform framework, where you have to depend on third-party implementations of some popular libraries. But this one is incredibly ridiculous. But, before diving into it, Lets take a brief look at how packages work in Flutter.
There are two ways to develop a flutter package that relies on native APIs:
1) The simple approach:
Put the platform-specific implementations in the package under the platform’s folder. See mixpanel-flutter.
2) The recommended way:
Separate out the platform-specific implementations into different packages. This is called a federated plugin in Flutter. It consists of the following:
The app-facing package, which the plugin’s user adds to their pubspec.
The platform-specific package(s) which has the native code, the app-facing package depeneds on these packages.
And the platform interface package, which declares a common interface that any platform package must implement to support the app-facing package.
For example, See the dependency tree of google sign in package
Now, let’s go back to the original issue. my_app is a flutter app targeting only Android & iOS. It depends on two packages flutter_facebook_auth & mixpanel_flutter . The full dependency tree looks like this:
Notice how adding a single package leads to a cascade of redundant dependencies meant for different platforms, which are not even the targets of my_app!
Observe that mixpanel_flutter depends on js ^0.7.0. While my_app also has a transitive dependency on facebook_auth_desktop which in turn has a transitive dependency on flutter_secure_storage_web! And since flutter_secure_storage_web uses js ^0.6.3, it’s incompatible with mixpanel_flutter. Thus causing the dependency conflict.
By now, you must have realized why this situation is so ridiculous. Why does my_app need to depend on facebook_auth_desktop and flutter_secure_storage_web at all, when it doesn’t even target desktop or web platforms? Because of this behavior, I am left to solve a dependency conflict arising out of redundant dependencies that will never be used in the project.
Thankfully, I found a hack to fix the issue in the package’s GitHub repo. Adding the latest version of the js package in the pubspec under dependency_overrides fixes the issue. Nevertheless, the problem should have never arisen; Flutter could have intelligently pruned the dependency tree by ignoring platform-specific dependencies not meant for my target platforms.