How to disable a specific analysis option rule in a folder - flutter

I have a project which can be simplified to:
lib/
|- main.dart
test/
test_environment/
|- test_test.dart
Where test_environment contains a set of "special" tests that cannot simply be run with
flutter test
They need some specific setting (for example: flutter test --dart-define=myVar=myValue).
In my lib/main.dart, I have something like:
class MyClass {
#visibleForTesting
MyClass.testConstructor();
}
I would like to use MyClass.testConstructor() from test/test_helper.dart. But when I do, I have a warning:
The member 'MyClass.testConstructor' can only be used within 'package:my_project/lib/main.dart' or a test. dart(invalid_use_of_visible_for_testing_member)`
I would like to disable this rule for the test_environment/ folder, but not for the lib/ folder. How can I do that?

The solution I found was to create a custom analysis_options.yaml for the folder test_environment/ that extends the root analysis_options.yaml:
lib/
|- main.dart
test/
test_environment/
|- test_test.dart
|- analysis_options.yaml
analysis_options.yaml
And in test_environment/analysis_options.yaml:
include: ../analysis_options.yaml
analyzer:
errors:
invalid_use_of_visible_for_testing_member: ignore # <- Disable the analyzer rule.
linter:
rules:
only_throw_errors: false # <- An example to show how to disable a linter rule.
This takes the analyzer/linter rules set by the root analysis_options.yaml and overrides the invalid_use_of_visible_for_testing_member to disable it in test_environment.

Related

How do I include and access package resources?

I have this folder structure:
my-package -
|- src/mypackage
|- resources/some-resources
|- setup.py
|- setup.cfg
I want to load some-resources from within mypackage. How do I do this?
I have read online I should update my setup.py to include:
setup(
...
package_data={"": ["resources"]},
include_package_data=True,
...
)
And that I should then be able to use pkg_resources to access my resource folder. But the examples seem like they are incomplete, e.g. pkg_resources.resource_listdir("mypackage", "") (one suggestion) just lists the python files in mypackage. So to reiterate - how do I include resources in my package, and then access them from my code?

how to ignore folders in melos packages

working in a monorepo with this structure
monorepo/
examples/
# ...
foo/
packages/
# ...
foo/
# ...
example/ -> examples/foo/
where the example in packages/foo
is a deep link to examples/foo/
that contains a demo app called foo_example
I'm encountering a problem during melos bootstrap
Run melos bootstrap
melos.yaml: Multiple packages with the name `foo_example` found in the workspace, which is unsupported.
To fix this problem, consider renaming your packages to have a unique name.
The packages that caused the problem are:
- foo_example at packages/foo/example
- foo_example at examples/foo
Error: Process completed with exit code 1.
I would like to keep the folders for examples and packages separated
while providing an example readily available for https://pub.dev/
is there a way to exclude the packages/**/example/ folders from melos?
this is how I'm pointing at the packages right now
name: <my_mono_repo>
packages:
- examples/**
- packages/**
scripts:
# ...
always read the docs :)
https://melos.invertase.dev/configuration/overview#ignore
this does the job
ignore:
# e.g. ignore example apps
- "packages/**/example"

How to ignore all lint rules for a file or a project?

I'm seeing too many lint warnings in my file, for example:
Avoid print calls in production code
Prefer const with constant constructors
Use key in widget constructors
I know how to disable a rule for a specific line or for the entire file, but I want to know is there any way I can disable all these lint warnings with something like
// ignore_for_file: all
According to the documentation,
// ignore_for_file: type=lint
should work. I don't have a project to test it right now though.
in your analysis_options.yaml file:
linter:
rules:
avoid_print: false
# add more here ...
# Available lints see: [https://dart-lang.github.io/linter/lints/index.html][1]
Unfortunately, there is no such comment like that. These lint rules are coming from flutter_lints package which is included in your analysis_options.yaml file, So, open the file and comment this line out:
// include: package:flutter_lints/flutter.yaml
After this, save the file using command + s
Just remove flutter_lints package from pubspec.yaml file.

Flutter: Package can't load a string resource using rootBundle.loadString - asset indenting correct

I have a project that consumes another package on the drive using a relative path:
project1 (setup as a full flutter project with flutter create project1)
project2 (setup with flutter create --template=package
project1's packages.yaml does this:
dependancies:
project1:
path: ../project2
project2's packages.yaml does this:
flutter:
assets:
- lang/en.json
Which works and everything sees everything else and there is no complaint about that path for the asset and I've verified that it has exactly 2 spaces before assets: and exactly 4 actual spaces beofre - lang/en.json
The problem occurs when project2 tries to load lang/en.json like this in code form project2:
final jsonString =
await rootBundle.loadString('lang/en.json');
I get an "asset could not be loaded ${key}" on the loadString function.
if however I take exactly the same code and put it on project1 and copy the folder exactly and copy the exact same asset tag in packages.yaml, project1 has no problem loading the file. If I even leave the asset links on the project1 then project2 can load them just fine too.
Is this a bug, or am I doing something wrong with the package template version?
I have same issue. and I found the solution:
In project2, do some steps as below:
create assets folder
create lang folder(or any other folder name)
create json file in lang folder. Ex: en.json, vi.json...
in pubspec.yaml of project, you need to declare assets:
flutter:
assets:
- assets/lang/vi.json
- assets/lang/en.json
when using loadString then the path will be :
await rootBundle.loadString('packages/language_pack/assets/langen.json');
Note that packages is plural and language_pack is package name

How to include file assets to be used by your own package in flutter

I have a package where I'm trying to load a string from my rootBundle. This file is to be used within the package itself only as part of internal configuration. I cant see where i'm going wrong.
This is my folder structure:
my-package
--> lib
--> pubspec.yaml
--> assets/file.js
This is the content of my pubspec.yaml
flutter:
assets:
- assets/
- assets/file.js
This is how i'm calling it
String js = await rootBundle.loadString('assets/file.js');
I keep on getting unable to load asset
Unable to load asset: assets/file.js
had the same problem but solved it by doing this:
In your package's .yaml file, add this line to import the file/asset
assets:
- packages/<Package name>/assets/<File name>
Replace the <Package name> with the name of your package, and <File name> with the name of said file/asset.
Then place the file/asset in a folder under your lib directory of your package. The structure should look something like this:
my_package
--> lib
--> assets
file.js
Make sure to add any additional folders to the reference if necessary.
When referencing the file/asset in your app or package, use the same reference as in you .yaml file:
String js = await rootBundle.loadString('packages/<Package name>/assets/file.js');
For example, here I'm getting a .js file.