How to suppress depend_on_referenced_packages just for a specific import, not the whole file? - flutter

After upgrading to Flutter 3.0 I started to get a lot of depend_on_referenced_packages lint issues when running flutter analyze. If I remove the reported imports the compilation breaks. Most of the issues are with package:collection/collection.dart which provides extension methods.
Currently the best I could find is to suppress this lint for the affected files by adding // ignore_for_file: depend_on_referenced_packages. However I like this lint (and in general I'd want to keep as much lints enabled as possible), so is there a way to suppress the lint only for a just a specific import and not for all imports in the file?
Relevant sections of pubspec.yaml:
...
environment:
sdk: ">=2.17.1 <3.0.0"
flutter: ">=3.0.1"
...
dev_dependencies:
flutter_lints: ^2.0.1
Running:
$ flutter analyze
...
info • Depend on referenced packages • lib/preferences/palette_spec.dart:3:8 • depend_on_referenced_packages
info • Depend on referenced packages • lib/ui/parts/circular_menu.dart:5:8 • depend_on_referenced_packages
...
Source code examples:
database_utils.dart (firstWhereOrNull is from collection.dart):
...
import 'package:collection/collection.dart';
...
Activity? _getActivityById(int id) {
return activities.firstWhereOrNull((element) => element.id == id);
}
...
Record? _getRecordById(int id) {
return records.firstWhereOrNull((element) => element.id == id);
}
palette_spec.dart (forEachIndexed is from collection.dart):
...
import 'package:collection/collection.dart';
...
paletteStr.split(",").forEachIndexed((index, colorStr) {
...
});
circular_menu.dart:
...
import 'package:vector_math/vector_math.dart' as vector;
...
final angle = vector.radians(90.0 / (widget.children.length - 1) * index + angleFix);
Note: the root cause is that collection is brought in as a transitive dependency.
Originally I misunderstood the lint. Explanation to Petr's solution: when he says "lint is reported if you depend on a transitive dependency" it means that somewhere in my code I have an import which imports stuff from that dependency. But at the time of the lint that dependency is only transitive, not direct. Therefore if I'd decide - for whatever reason - to not depend on the package which brings that in then suddenly I'd have an error out o the blue for that import. The lint tries to make that dependency graph more direct.

This lint is reported if you depend on a transitive dependency. Do you have in your pubspec.yaml defined collection?
dependencies:
collection: ^1.16.0
I had same issue but after defining dependency, lint issue is gone.

Related

Flutter, AmplifyStorageS3 plugin not added correctly

getting
Error :
/*
dynamic AmplifyStorageS3()
package:my_budget_app/main.dart
AmplifyStorageS3 isn't a type.
Try correcting the name to match an existing type
*/
I am following this docs
https://docs.amplify.aws/lib/storage/getting-started/q/platform/flutter/#provision-backend-storage
Amplify storage S3 is not imported correctly.
I cleared cache and reinstalled all the pubs
I deleted publock file and pub get all the files no luck
I restarted vscode a couple of times no luck
I see the import says not used so AmplifyStorageS3 class is not exported from the package
ackage:amplify_storage_s3/amplify_storage_s3.dart ???
I have 3 ideas.
1: In package import import 'package:amplify_storage_s3/amplify_storage_s3.dart'; add "as ampl" to the end and then you will have something like this:
import 'package:amplify_storage_s3/amplify_storage_s3.dart' as ampl;
And then write:
ampl.AmplifyStorageS3 storage = ampl.AmplifyStorageS3();
2: Hold ctrl and left click on the imported package, find this class in the package and find out what's wrong.
3: Check your pubspec.yaml and so on for minimum requirements:
environment:
sdk: ">=2.12.0 <3.0.0"

No such module in a Swift Package using Xcode - package listed in dependencies

I create a blank template package:
> swift package init --name Temp
> open Package.swift
Xcode Version 13.2.1 (13C100) opens the package.
I add a dependency to the package.
dependencies: [
.package(url: "https://github.com/johnsundell/publish.git", from: "0.7.0")
],
Xcode > Product > Build succeeds at this point.
I edit Temp/Sources/Temp/Temp.swift to insert the first line the package that is defined in dependencies.
import Publish
A build now generates the following error:…/Temp/Sources/Temp/Temp.swift:1:8: error: no such module 'Publish'.
I feel certain this is an Apple bug. Or I could be missing something.
There are several posts about this problem when there is an xcodeproj and the additional structure that provides. Some of them hint at workarounds that help some people.
Has anyone seen this and/or know of how to resolve it?
Apple's Creating a Standalone Swift Package with Xcode document doesn't provide any insight.
thanks for the chatter in the comments, #Larme & #koen, it helped
The issue was user error (and/or a documentation lapse). Living on the (bleeding) edge.
Sometimes updates from changes are slow or require a clean or a relaunch.
Xcode auto-generates Schemes from the targets defined in your package. My build was targeting MyTarget.
Two things were missing:
name: "Publish" was not included in the package dependency - it's needed so you can reference it below (or maybe this can be derived, it's hard to tell because of Xcode refresh issues), and
a reference is needed in the dependencies for each target using the package-dependency, i needed to add dependencies: ["Publish"] in the related target
dependencies: [
.package(name: "Publish", url: "https://github.com/johnsundell/publish.git", from: "0.7.0")
],
…
targets: [
.target(
name: "MyTarget",
dependencies: ["Publish"]),
]

How do I make a shared directory to share code in flutter between integration tests and widget tests?

How do i create a directory of shared code between Integration tests and Widget tests in flutter? There could be quite a bit of it since they use the same API now as of the new official
in Android native you can do the below in order to have code that is shared between local non-instrumented UI tests (Roboelectric under the hood) and instrumented Espresso tests with the Espresso API. And of course miscellaneous shared helpers, mocks, whatever.
How do i achieve a similar goal in Flutter?
// This allows us to share classes between androidTest & test directories.
android.sourceSets {
test {
java.srcDirs += "$projectDir/src/testShared"
}
androidTest {
java.srcDirs += "$projectDir/src/testShared"
}
}
This could be achieved using a shared package (namely test_base).
Create a package for your test codes (confusion avoidance: your code will be written in its lib folder).
run:
flutter create --template=package test_base
Add your main package as a dependency to test_base so you have access to your main package codes.
test_base/pubspec.yaml:
dependencies:
<your-package-name>:
path: ../
Add test_base to your main library as a dev-dependency.
./pubspec.yaml:
dev_dependencies:
test_base:
path: test_base/
Now you can import the test_base package in your widget/integration tests.
Note: This way within test_base you can import packages of transitive dependencies (dependencies of your main package) without a compilation error. But it is discouraged in dart to explicitly import packages of dependencies you don't explicitly define in test_base/pubspec.yaml and it's probable to face linter or compilation warnings in future versions of dart (currently it's 2.13.4 and no warnings). Which means then you need to explicitly define those dependencies in test_base/pubspec.yaml. Though I think this is the case it would be harmless to import transitive dependencies packages.

Dart any dependency usage in pubspec.yaml for multi module project

I need something similar in Dart to what Maven has with a parent-pom dependencyManagement structure.
I have 2 modules in Dart, api and core.
api/pubspec.yaml:
...
dependencies:
http: '>=0.12.0 <0.13.0'
meta: '>=1.0.0 <2.0.0'
...
core/pubspec.yaml:
...
dependencies:
api:
path: ../api
# Meta is added again with a version number
meta: '>=1.0.0 <2.0.0'
redux: '4.0.0'
...
So if I change the dependencies in core/pubspec.yaml to 'any' if they already exist in api/pubspec.yaml:
...
dependencies:
api:
path: ../api
# No version number is needed, the exact version will come from the dependency
meta: 'any'
redux: '4.0.0'
...
This seems to be working, but the official site does not mention this feature, instead:
The string any allows any version. This is equivalent to an empty version constraint, but is more explicit. Although any is allowed, we don’t recommend it.
Is this an unintended good use case of the 'any' or this is a bad idea? (I am confident api dependency will always exist and declare meta, they are all my modules.)

Flutter How to import backendless_sdk messaging module?

I am following this article
How to Create a Chat App with Backendless SDK for Flutter
import 'package:flutter/material.dart';
import 'package:backendless_sdk/backendless_sdk.dart';
import 'package:backendless_sdk/src/modules/modules.dart';
There is a error:
"Target of URI doesn't exist: 'package:backendless_sdk/src/modules/modules.dart'."
The modules.dart import is required for Backendless.Messaging, but without the import there is an error:
The getter 'Messaging' isn't defined for the type 'Backendless'.
void initListeners() async {
Channel channel = await Backendless.Messaging.subscribe("myChannel");
channel.addMessageListener(onMessageReceived);
}
pub spec.yaml
dependencies:
flutter:
sdk: flutter
backendless_sdk: ^1.1.8
How can I import the Messaging module?
You should change from:
await Backendless.Messaging.subscribe
in to:
await Backendless.messaging.subscribe
^
|
small "m" here
Versions
I checked backendless_sdk: ^0.0.2 (from tutorial) and backendless_sdk: ^1.1.8 (newest one), and in both cases this field was named messaging (lowercase).
Class Backendless
backendless_sdk-1.1.8/lib/src/backendless.dart:
It look like you are missing the installation step.
From the terminal: Run flutter pub get.
OR
From Android Studio/IntelliJ: Click Packages get in the action ribbon
at the top of pubspec.yaml.
From VS Code: Click Get Packages located
in right side of the action ribbon at the top of pubspec.yaml.
I do not see any references to modules.dart in the article you mentioned. You need to import backendless_sdk and also include the socket.io dependency:
dependencies {
implementation ('io.socket:socket.io-client:1.0.0') {
// excluding org.json which is provided by Android
exclude group: 'org.json', module: 'json'
}
}