Unexpected Flutter linter warning: depend_on_referenced_packages

2.8k views Asked by At

After I recently upgraded flutter to 3.0.0 and flutter_lints to 2.0.1, I started getting the following warning:

Depend on referenced packages.

Here, the dependency that is imported is defined in the pubspec.yaml of another module that the current module already depends on. In other words, the dependency flow is as follows:

A (module that gives warning and imports D) -> B -> D

I don't understand why I see this warning. Just because module A doesn't directly depend on library D? What should I do with this warning? How can I eliminate it without ignoring the rule in analysis_options.yaml file?

7

There are 7 answers

1
더테크빌개발팀 On

analysis_options.yaml

include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

linter:
  rules:
    depend_on_referenced_packages: false
1
MLZ On

Had the same warning.

In my case it was caused by flutter_localizations.

I realized I put

flutter_localizations:
    sdk: flutter

below dev_dependencies and not below dependencies in pubspec.yaml.

Maybe you have the same or similar problem.

2
lime On

You're seeing the intended behavior of that lint. Without that lint you would get an error if the included library was not depended upon, and you rightly assume that a transitive dependency is enough to build.

The lint is likely here to protect against the cases where a dependency has either removed the dependency or changed to a version with breaking changes, such that your transitive use would either break or unintentionally change.

Each of the lints contain a brief description of why they exists.

0
Denis Rudov On

I had all the records in pubspec.yaml In my case it was caused by the Android Studio itself. I had to restart it and all was started to work fine

0
Imdat Solak On

In my case, they were all packages that were required (and thus included) by other packages that were listed in pubspec.yaml but since I was using them myself directly, I had to explicitly add them to pubspec.yaml as direct-dependents.

Once I added the offending packages to pubspec.yaml, error was gone.

0
Maureen Josephine On

This error recommends that in pubspec.yaml file, you add the package in the dev-dependencies or dependencies(depending on the use case as instructed on the package docs). Checkout this

enter image description here

Also, make sure you are importing the right package in the right format as required to in the package documentation.

1
user1603721 On

Check to make sure the package is under "dependencies" and not (for example) under "dev_dependencies"