Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart Packages: Dependency conflicts

In a Dart/Flutter project, I have

dependencies:
  graphql: '^2.1.1-beta.5'
  flutter_bloc: ^3.0.0

as dependencies. However, graphql depends on rxdart: ^0.22.0 and flutter_bloc depends on rxdart: ^0.23.0, which give me error:

Because flutter_bloc >=3.0.0 depends on bloc ^3.0.0 which depends on rxdart ^0.23.0, flutter_bloc >=3.0.0 requires rxdart ^0.23.0.
And because graphql 2.1.1-beta.5 depends on rxdart ^0.22.0 and no versions of graphql match >2.1.1-beta.5 <3.0.0, flutter_bloc >=3.0.0 is incompatible with graphql ^2.1.1-beta.5.
So, because com.myapp depends on both graphql ^2.1.1-beta.5 and flutter_bloc ^3.0.0, version solving failed.

My temporary fix is to downgrade flutter_bloc to ^2.1.1, which uses rxdart ^0.23.0. But if I want to use the latest and greatest version of flutter_bloc, what is the best solution?

Thanks!

like image 456
nigong Avatar asked Dec 31 '19 22:12

nigong


2 Answers

Disclaimer: I am not the owner of the answer. I am just posting answer from reference: https://iiro.dev/resolving-dart-package-version-conflicts/

This helped me.

Let's say you have problem like this:

Because intl_translation 0.17.0 depends on petitparser ^1.1.3 and xml >=3.2.0 
depends on petitparser ^2.0.0, intl_translation 0.17.0 is incompatible with xml >=3.2.0.

So, because my_project depends on both xml ^3.2.0 and intl_translation 0.17.0, 
version solving failed.

The pubspec file might look a little something like this:

pubspec.yaml
dependencies:
  # ...
  xml: ^3.2.0
  intl_translation: ^0.17.0

Solution: The fastest way to resolve this problem is to set the versions of both of the conflicting dependencies to any.

pubspec.yaml
dependencies:
  # ...
 xml: any # <- don't leave me like this - read further!

 intl_translation: any # <- don't leave me like this either!

^^This is not solution, there is one more step!

The output might like below:

Resolving dependencies... 
Got dependencies!

After getting the project to build, you should tighten the dependency versions back to use semantic versioning like they previously did. Open the generated pubspec.lock file and find the dependencies that were previously conflicting.

pubspec.lock
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
packages:
  xml:
    # ...
    version: "3.0.1" # the version of "xml" package that worked fine
                     # with "intl_translation".
  intl_translation:
    # ...
    version: "0.17.0" # the version of "intl_translation" package
                      # that worked fine with "xml".

From that lockfile, we can see that the xml package version 3.0.1 and intl_translation package version 0.17.0 play along together well. As the last step, replace any with the correct versions on your pubspec file:

pubspec.yaml
dependencies:
  # ...
  xml: ^3.0.1
  intl_translation: ^0.17.0

Refetch your dependencies one last time by running flutter packages get to verify that this does indeed work and then you’re good to go.

Answer credits: Iiro Krankka

like image 142
AkshayT Avatar answered Oct 03 '22 03:10

AkshayT


You can use dependency overrides in pubspec.yaml:

dependency_overrides:
  rx_dart: ^0.23.0

Note that plugins that depend on another version of the dependency you override can break if you do this.

like image 45
creativecreatorormaybenot Avatar answered Oct 03 '22 02:10

creativecreatorormaybenot