33

On my Flutter app, I've got this problem with lint rule : depend_on_referenced_packages

enter image description here

This file is generated here

enter image description here

Do you have any idea how to solve this without passing by ignore 'depend_on_referenced_packages' ?

in my pubspec.yaml, I only have this :

enter image description here

Thanks a lot

NonowPoney
  • 982
  • 4
  • 14
  • 30

9 Answers9

36

Just add the package explicitly.

  1. run flutter pub add flutter_gen
  2. run flutter pub get

For more info, see https://dart-lang.github.io/linter/lints/depend_on_referenced_packages.html

Akbar Masterpadi
  • 976
  • 8
  • 15
Yusuke Hakamaya
  • 3,580
  • 1
  • 7
  • 9
  • This works. And I really like this sollution since it solves the problem instead of just hiding the error. But I'm still wondering what causes the lint? – Aymen Dennoub Jul 19 '22 at 05:08
  • 4
    @AymenDn Firstly, _refenrenced packages_ are packages referenced by other packages you explicitly depend on, as shown in `pubspec.yaml`. It works without explicitly importing because these packages are also cached, but it's hard to know the package's information, e.g., versions you're using. Hence, they recommend doing this explicitly. – Yusuke Hakamaya Jul 19 '22 at 07:45
  • I happen to have this issue just now. For me, I accidentally deleted the `dependencies:` line in my **pubspec.yaml** file. Simply restoring that got things back to normal. – berkaykurkcu Apr 04 '23 at 17:58
6

I don't know how to ignore only generated packages, but this is how you turn off the rule entirely:

  1. Create a file analysis_options.yaml in your project root.
  2. Paste this:
include: package:flutter_lints/flutter.yaml

linter:
  rules:
    depend_on_referenced_packages: false

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

EzPizza
  • 979
  • 1
  • 13
  • 22
5

Based on the documentation, it seems that just having a dependency on flutter_localizations only is not enough. Your dependencies should look like this:

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: ^0.17.0 # Add this line
mkobuolys
  • 4,499
  • 1
  • 11
  • 27
4

For me, this error occurs because I accidentally put my package name in the dev_dependencies section. Putting my package name in dependencies section fixed my problem.

3

I'm using collection.dart more than a dozen places in my app and recently after the Flutter 3.0 upgrade this lint started to pop up for me. I think depend_on_referenced_packages is a really useful lint to warn you about unnecessary imports. I would never turn it off entirely for the project (@EzPizza's answer).

You can suppress the lint for an affected file by adding this comment to the file:

// ignore_for_file: depend_on_referenced_packages

But it's even better to fix the root cause of the problem and include the indicated dependencies into your pubspec.yaml. Like for example what @mkobuolys proposes.

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
3

For me, I accidentally put the package url_launcher: ^6.1.7 under dev_dependencies:

Solution:

I removed the package from dev_dependencies and added it below dependencies:

Diraph
  • 147
  • 8
0

I'm only posting this because the other answers either didn't help me or I didn't understand them.

I got the same warning on a different package, namely page_transition. Here's what I found on why the problem occurs:

When importing a package, add a dependency on it to your pubspec.

Depending explicitly on packages that you reference ensures they will always exist and allows you to put a dependency constraint on them to guard you against breaking changes.

Meaning you are right to look for a solution other than to ignore it.

Here's how I fixed it: I went to pubspec.yaml and added the following under dependencies:

dependencies:
  page_transition: ^2.0.9

You can add the dependency for your package which you would have to look up. I just googled "flutter app_localizations" and this came up, you can see the latest version under the Installing tab. localization 2.1.0

So, if you're using that package, as an example, you would have to add

dependencies:
  localization: ^2.1.0
M Ibrahim
  • 1
  • 1
-1

for me it was because the packages i was having this warning on didn't support flutter 3 yet.

Mezo
  • 9
  • 4
-1

this is the official link

you should direct dependency not indirectly dependency.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
xinxin
  • 1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32102227) – CbL Jun 29 '22 at 09:52