How to avoid lint problem "Depend on referenced packages" for gen_l10n/app_localizations.dart

Viewed 7357

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

7 Answers

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

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

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.

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

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
Related