How to fix: Because ... depends on ... which depends on ..., ... is required
Learn the cause of this common error in Flutter and what steps to follow to solve it
You already have your application almost finished, most of the functionalities are developed and tested, you have everything ready to be able to deploy the app to the stores. Then you think on giving a final brush to your app by updating the dependencies of pubspec.yaml to launch your app with the latest improvements available...you go to pub.dev to see if there are new versions of the packages that your application uses, you update the old libraries and then...
Because your_app depends on [dependency1] 1.0.0 which depends on [dependency2] ^2.0.0+2, [dependency2] ^2.0.0+2 is required
Most likely, even the error you are seeing is even more convoluted, with the error trace listing endless dependencies that depend one on another and the other on the previous one, and so on... something with which Flutter developers have had to get used to it over the years and what you have to deal with from time to time: the so called dependency hell.
Why does this error occur?
In this article I am going to explain the cause of this common error and how to solve it, but first we have to define some basic concepts:
To begin with, the concept of dependency: a dependency is an isolated component that you can use to add functionality to your application. In Flutter these types of components are called packages and plugins. Packages are projects written entirely in Dart language that can be used in both Flutter and pure Dart projects, while plugins have, in addition to Dart code, embedded native code.
When we talk about the dependencies of an application, we are talking about all that collection of packages and plugins which your application needs to work, that is: those packages and plugins defined in a project's
In this graph we can see how your application has the
Dependency 1 and
Dependency 2 packages installed. So both libraries are dependencies of your application.
What is a transitive dependency?
The next important concept to understand here is the transitive dependencies, that is, those dependencies that your dependencies depend on.
It can be somewhat confusing to understand at first, let's see it better with a graphical example:
Here we can see a situation similar to the previous one, however there is a third library,
Dependency 3, that
Dependency 1 uses, with which:
Dependency 2are still considered dependencies of your application, exactly the same as before.
Dependency 1depends on
Dependency 3, so your application, albeit indirectly, also depends on
Dependency 3. And this type of "indirect" dependency is what we refer to as transitive dependency.
No matter how big or deep this dependency tree is, your application will always depend on the packages and plugins you have installed and also on all those packages and plugins on which the latter ones depends on.
So the cause of the error is the transitive dependencies?
Before answering this question, let's look at the following situation:
In this graph we can see that your application depends on
Dependency 1 and
Dependency 2, the same as in the previous cases. Now, in this case, both depend on
Dependency 3, but with the particularity that
Dependency 1 depends on version 1 and
Dependency 2 on version 2.
In conclusion: your two direct dependencies depend on a transitive dependency, however the dependency is towards different and mutually incompatible versions. And the result is the error that has brought us all here.
So, to the question: the cause of the error is always incompatibilities with transitive dependencies? Well, generally yes, it usually happens when you update a version of a library that in turn has a transitive dependency on another library, while you (or a dependency of yours) uses another version of that same library.
How do I fix the error?
Let's see how we can solve this error through a practical example. Let's imagine that we have an app that has the following dependencies:
If we execute a
flutter pub get we will get the following error:
Because your_app depends on floor 1.0.1 which depends on sqflite ^2.0.0+3, sqflite ^2.0.0+3 is required. So, because your_app depends on sqflite 1.3.2+4, version solving failed.
This error message can be decrypted with the following:
- That your application depends on version 1.0.1 of
floor, and that this in turn depends on version 2.0.0+3 of
- Therefore, version 2.0.0+3 of
sqfliteis an essential requirement to be able to compile your app
- That, since your app depends instead on version 1.3.2+4 of
sqflitethe dependency resolution fails and cannot continue (because two different versions of the same dependency cannot coexist within a Dart project)
So the solution is to try to "balance" the versions against each other until all dependencies, including transitive ones, are compatible with each other. I would like to tell you that there is some command or magic formula to solve this problem with a single click, but as far as I know, you have to do it manually.
Normally, package and plugin maintainers try to keep their applications up to date, so most of the time this type of error is solved by having all your dependencies updated to recent versions.
In cases where this is not the case, it is possible that the cause is a dependency that has been left behind... in that case you will have no choice but to look at the source code of your dependencies, see what packages and plugins they depend on, and try to contact with the project maintainer to see if they can update that conflicting dependency.
Other times the solution is to not update a dependency to prevent this error from occurring and wait for the package with legacy dependencies to be updated.
In conclusion: my recommendation is that you carefully read the error that Flutter gives you to find out the conflicting dependency, and what other dependencies depend on it. And from this point, you can go to the release notes of those projects, see what changes they have, or visit the issue trackers to see if someone has already complained about this and if it's planned to update the dependencies.
That's all for today, I hope this article has helped you at least a little to solve your dependency issues. Good luck with it!