How to import file from lib into file outside of lib in Flutter - flutter

I have customised a Flutter package and therefore it's outside of my lib folder.
Structure:
project_name
lib
file.dart
package
package_file.dart
I want to import file.dart into package_file.dart. How can I achieve this?
Tried import 'package:project_name/lib/file.dart' but it says URI not exist.

You should have pubspec.yaml in your package module and declare your project root file packages yaml's dependencies like this :
dependencies:
ROOT_PROJECT_NAME:
path: ../../ROOT_PROJECT_NAME

Related

how to import local package files in my main package?

I want to import the about.dart file in my main.dart file. But it is showing error while I'm copying its path. I tried many ways to import it, but all fails. Can anyone please help me in this?
if both the files are in the same folder you can import them using this line
import './dialpad.dart';
if you want to import the file from a folder at the same level of the file in which you want to import you can use this line
import '../widgets/some_file.dart';
To use your local package you have to define package and give a package path inside your pubspec.yaml for the package.
To add local package in pubspec.yaml:
sip_up-0.5.6:
path: ./sip_up-0.5.6

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

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 package can not find classes in package

I am trying to create a flutter package.
Inside Lib(package) folder, I have MY-PACKAGE.dart file. I also have a folder Called src which contains some codes I will import for use in MY-PACKAGE.dart.
In my pubspec.yaml in Example folder I have added the package like below and run packages get.
onboardly:
path: ../
To use my package I do this
// THERE IS NOT ANY PROBLEM WITH THIS IMPORT STATEMENT
import 'package:onboardly/onboardly.dart';
OnBoardly( // THIS CLASS WORKS FINE
screens: [
OnBoardlyScreenItem( // THIS CLASS CAN NOT BE FOUND EVEN THO IT EXISTS IN THE src FOLDER
image: Image.asset("assets/loadicon.png"),
description: Text("Hello There"),
),
],
),
The problem am facing is the fact that OnBoardlyScreenItem() which is in the src of the package can not be found.
I have ran
flutter packages get
flutter pub get
restarted my IDE
run flutter clean
Adding this for anyone later,
I had to export those classes or files that I wanted to be available to the Package to the "Entry file" since the folders in my case are regarded as private. Export all files or classes you'll want to be available to the user.
// Exporting all codes to be avaible to package
export 'package:onboardly/src/IntroScreen/OnBoardlyScreenItem.dart';

Target URI doesn't exist, when trying to import a package in 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