Skip to content

Auto import for list firstWhereOrNull method throws a warning. #3900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
safield opened this issue Apr 1, 2022 · 7 comments
Closed

Auto import for list firstWhereOrNull method throws a warning. #3900

safield opened this issue Apr 1, 2022 · 7 comments

Comments

@safield
Copy link

safield commented Apr 1, 2022

When using the list extension method firstWhereOrNull, the dart-code extension only suggests the import statement..

import 'package:collection/src/iterable_extensions.dart';

When selecting this option, the extension gives the warning for the resulting import line...

Don't import implementation files from another package.

The correct import import 'package:collection/collection.dart'; is not suggested.

@safield safield added the is bug label Apr 1, 2022
@DanTup
Copy link
Member

DanTup commented Apr 4, 2022

@safield can you confirm the version of the SDK you're using? This looks like dart-lang/sdk#47842 which seems to have been fixed, although I'm not certain if it's in the SDK you're using yet.

@DanTup DanTup added the awaiting info Requires more information from the customer to progress label Apr 4, 2022
@safield
Copy link
Author

safield commented Apr 4, 2022

I did update version a day ago and now there is no incorrect recommendation. But there is also no correct recommended code action either.

@DanTup
Copy link
Member

DanTup commented Apr 5, 2022

@safield can you confirm your SDK version and perhaps provide a small repro? In a simple test, it does seem to show up for me:

Screenshot 2022-04-05 at 09 17 12

@safield
Copy link
Author

safield commented Apr 7, 2022

Flutter 2.10.4 • channel stable • https://github.com/flutter/flutter.git
Framework • revision c860cba910 (2 weeks ago) • 2022-03-25 00:23:12 -0500
Engine • revision 57d3bac3dd
Tools • Dart 2.16.2 • DevTools 2.9.2

Running on Ubuntu 21.10 with flutter install through the official snap package.

firstWhereOrnull

@DanTup
Copy link
Member

DanTup commented Apr 7, 2022

Is package:collection definitely listed in your pubspec.yaml and you've fetch packages since adding it?

If so, are you able to share a small sample project this definitely occurs for you in that I can test with?

@safield
Copy link
Author

safield commented Apr 8, 2022

It is not added to my pubspec.yaml.

I assumed it was a package that was part of the default SDK packages. I do not have it in my pubspec.yaml, but adding the following line to a dart file compiles and runs without error or warning...

import 'package:collection/collection.dart';

@DanTup
Copy link
Member

DanTup commented Apr 8, 2022

Ah, that will be the issue. package:collection is just a Pub package, it's not included in the SDK.

The reason it works at runtime is that the package is being referenced by some other dependency, so the runtime is able to resolve it and find the source. However the analysis server (which provides the language features) will not suggest imports for packages you have not explicitly listed in pubspec.yaml, because it would add additional (undeclared) dependencies to your code.

If you depend on a package, it should always be listed explicitly in your pubspec.yaml.

If you want to get a warning when you depend on something not in your pubspec (such as if you import 'package:collection/...';), you can enable the depend_on_referenced_packages lint.

Hope this helps!

@DanTup DanTup closed this as completed Apr 8, 2022
@DanTup DanTup removed is bug awaiting info Requires more information from the customer to progress labels Apr 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants