Target URI doesn't exist, when trying to import a package in flutter - flutter

I have stumbled upon a problem in importing the package in Flutter, I tried to solve this by running flutter packages get and also shutting down the project in Android studio and reopening it.
import 'package:task_02_category_widget/category.dart';
Here is the line above, and the error I'm running into when I run it gives the following error in the console.
Your application could not be compiled, because its dependencies could
not be established.
The following Dart file:
/Users/username/Documents/flutter_rectangle_2/lib/main.dart
...refers, in an import, to the following library:
package:task_02_category_widget/category.dart
That library is in a package that is not known. Maybe you forgot to
mention it in your pubspec.yaml file?

If task_02_category_widget/category.dart is part of an old project you are reusing you should put it in a folder in your flutter application and include it like "../ folder /task_02_category_widget/category.dart ". If it is part of github repository you have copy pasted from, just copy the file and use the step above. Most probably you are looking for that . In any other case check here to find the source code.

You should have in your project at a file called pubspec.yaml a definition like this:
name: my_app
dependencies:
task_02_category_widget:
Let’s say that your package is laid out as follows:
task_02_category_widget/
lib/
category.dart
Then, you can import it:
import 'package:task_02_category_widget/category.dart';
More information:
https://www.dartlang.org/tools/pub/get-started
https://www.dartlang.org/guides/libraries/create-library-packages

Related

Internationalization in a monorepo

I have an app (just frontend) whose code is structured in a monorepo. There are separate packages for each custom widget. I would also like to have one package with all the translations. In this way, if another package need a translation I just need to import that package. So I created the translation package and in its main file I just wrote (here I use intl):
export 'package:flutter_gen/gen_l10n/app_localizations.dart';
Now, if I import that package in another package and I import the file with:
import 'package:l10n/l10n.dart';
It tells me that that import is not used and gives me error when I use the translation with AppLocalizations.of(context)!.foo
If you use VScode (don't know for other editors) you have to manualy import the generated translation package:
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
It seems that it's not possible to export generated file by design (check out this github issue). The best solution that I've found to fix my problem is the following:
Create a translation package inside your project, like in packages/translations and add that package to the pubspec.yaml of your your main project. Like this:
dependencies:
flutter:
sdk: flutter
translations:
path: packages/translations
In that package create a l10n.yaml file like this:
arb-dir: lib
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: false
If you are working in a git repo, add packages/translations/lib/app_localizations*.dart in your .gitignore
Now if you run flutter packages get && flutter gen-l10n in that translations packages all the translations will be automatically generated (assuming they are in packages/translations/lib/app_xx.arb)
Now you can import the translation file from you main package with import 'package:translations/app_localizations.dart';
The only drawback is that the translations are not automatically generated when you type flutter pub get (see this issue). In order to regenerate them you have to type flutter gen-l10n inside the translations package every time. The solution could be improved by using a tool for managing Dart monorepo projects like melos.
An example implementation of what I described (including melos) could be found in this github repo

ObjectBox generator warning:"Failed to find package root from output directory, generated imports might be incorrect."

Iam using the latest version of objectbox and when i run the "flutter pub run build_runner build" command i get this output: "...Failed to find package root from output directory, generated imports might be incorrect..."
So when i write in main to init ObjectBox:
late ObjectBox objectBox;
ObjectBox is not recognized with import.
Using the import from an tutorial which have the same version does not solve the problem,but the json and g.dart files are generated.
What iam doing wrong?
Let me know if you need more information!
Thanks
You will get the error you are seeing if your ObjectBox files are in the wrong directory. By default, ObjectBox expects objectbox.dart, objectbox.g.dart, and objectbox-model.json to be at the root of your project (i.e. in the lib directory next to the main.dart file. You can move objectbox.dart though. I typically put it in lib/objectbox, but I think that objectbox.dart and the generated files need to exist in the same directory, though I'm not 100% sure about that. Change the directory where objectbox.g.dart and objectbox-model.json are generated by following the instructions below from the ObjectBox README.md. If you wanted to put the files in lib/objectbox you would just change custom to objectbox in the code snippet below:
To customize the directory (relative to the package root) where the
generated files are written, add the following to your pubspec.yaml:
objectbox:
# Writes objectbox-model.json and objectbox.g.dart to lib/custom (and test/custom).
output_dir: custom
# Or optionally specify the lib and test output folder separately.
# output_dir:
# lib: custom
# test: other

Couldn't Import a dart file in a new dart file (Target of URI doesn't exist)

My flutter project has a separate dart file for each functionality, So, I tried to import a dart file in a new dart file to access the functionality in that file, but I got this error "Target of URI doesn't exist: 'package:filename.dart'. Try creating the file referenced by the URI, or Try using a URI for a file that does exist"
I tried solutions like restarting the Ide, flutter clean command and pub get, Most of the solutions for the same error have to deal with packages, it can be solved by adding package name in dependencies. But this is a dart file, how do I solve it.
When using the import function and going to the root folder with 'package:*', you need to first write the flutter project folder name, and then after that the file or subfolders.
All this but skipping the 'lib' folder.
As an example:
import 'package:flutterProject/subFolderInLib/filename.dart'
Hope this was understandable and worked! :)

Error: Could not resolve the package 'app' in 'package:app/app_controller.dart

In my last flutter project, I included files placed in my lib/ directory by using import 'package:app/file_name.dart'.
For example, my file located at lib/app_controller.dart was imported via:
import 'package:app/app_controller.dart'
I just started a new flutter project, and it is giving me the error:
Error: Could not resolve the package 'app' in 'package:app/app_controller.dart'
When I remove the portion package:app/, it builds fine. I find this very strange because my previous project is still building just fine without any changes. Does anyone know what's happening here?
What is allowing my old project to respect package:app/..., but not my new project?
package:app/ would work only for an application that is called app. What is the name of your new application? When you import items from your own project, it goes like this:
package:{{YOUR APPLICATION NAME}}/{{DIRECTORIES}}
What is the name in your pubspec.yaml file? It's usually on the very first line.
it is because a dart class can be imported in two ways(AFAIK),
local import from the root of current file where import is being used for example import '../folder/file.dart
with a package name which should begin from package for example import package:packagename/any_file_in_the_lib_folder.dart, a package will have a pubspec.yml which defines a package name which will be used to import the content of the lib folder of that package, in your case your first project is named app so it respected this import style but your second project isn't respecting it because its not named app but something else.

Flutter NFC reader plugin does not work with error inside plugin

I'm trying to make a flutter version of my old app , it needs NFC .
First I create a default project for test run and it works fine.
I tried to use flutter-nfc-reader and install it following "Installation" in that default project.
And here comes the problem:
After I edit "pubspec.yaml" and do "packeges get" , it automatically edit "GeneratedPluginRegistrant" under "myapp/android/app/src/main/java/io.flutter.plugins".
added
import it.matteocrippa.flutternfcreader.FlutterNfcReaderPlugin;
and
FlutterNfcReaderPlugin.registerWith(registry.registrarFor("it.matteocrippa.flutternfcreader.FlutterNfcReaderPlugin"));
But the import can't find that package "it.matteocrippa.flutternfcreader" which is inside the plugin.
I tried just copy that package but that file will be full of error when it is inside my project.
I have no idea how to fix it....
As Github reads:
last step import to the project:
import 'package:flutter_nfc_reader/flutter_nfc_reader.dart';