Create a package in dart - flutter

How do I create a package in the new Dart Editor?
There is no "Add Pub support" checkbox?
Also how to create "packages" with the new editor?
Is a tutorial out there that describes the process with the new Editor?

To create a package named mypackage.
For Dart package:
dart create --template=package-simple mypackage
For Flutter package:
flutter create --template=package mypackage

From the Dart/Flutter Documentation:
Step 1: Create the package
To create a Flutter package, use the --template=package flag with flutter create:
flutter create --template=package hello
This creates a package project in the hello folder with the following content:
LICENSE
A (mostly) empty license text file.
test/hello_test.dart
The unit tests for the package.
hello.iml
A configuration file used by the IntelliJ IDEs.
.gitignore
A hidden file that tells Git which files or folders to ignore in a project.
.metadata
A hidden file used by IDEs to track the properties of the Flutter project.
pubspec.yaml
A yaml file containing metadata that specifies the package’s dependencies. Used by the pub tool.
README.md
A starter markdown file that briefly describes the package’s purpose.
lib/hello.dart
A starter app containing Dart code for the package.
.idea/modules.xml, .idea/modules.xml, .idea/workspace.xml**
A hidden folder containing configuration files for the IntelliJ IDEs.
CHANGELOG.md
A (mostly) empty markdown file for tracking version changes to the package.

There's no such possibilty in the Dart Editor for now. To create a package follow these steps :
create an New Application mylib without sample content
add a pubspec.yaml file
add a lib folder
create a mylib.dart containing the code you want to package
See the Package layout conventions for more informations.

You can create a dart project following the flutter way that allow you to auto generate the structure and the hierarchy of the package.

Follow below steps to create a package in DART:
Step 1: Create the package
$ flutter create --template=package hello
Step 2: Implement the package
For pure Dart packages, simply add the functionality inside the main lib/.dart file, or in several files in the lib directory.
To test the package, add unit tests in a test directory.
For additional details on how to organize the package contents, see the Dart library package documentation:
https://flutter.dev/docs/development/packages-and-plugins/developing-packages

Any dart app is a package. To create a new Dart app use:
dart create my_package

Related

Not a valid Dart package name - Flutter error

I want my app to be available for the web.
I run this command: flutter create .
I get this error: "Fredi" is not a valid Dart package name.
I know that there shouldn't be any capital letters. However, I do not know how to change the dart package name.
I have already used these packages successfully, but they seem to be changing other things, not the Dart Package Name.
change_app_package_name: ^1.1.0
rename: ^2.0.1
The only thing in my project with the name 'Fredi' is the root folder (original project name). However, I don't know how to change it (file explorer won't let me) and it will probably break things.
What step can I take?
Config Files
app/build.gradle -> applicationId "com.tomasward.fredi"
android manifest -> package="com.tomasward.fredi"
android manifest -> android:label="Fredi"> [I understand this is the name displayed to the user, I've tried changing it but it doesn't fix the error.]
You can run flutter create --project-name "your_valid_package_name" InvalidPackageNameDirectory to create a Flutter project in a directory with a non-valid package name.
In an already existing project, you can change the package name by modifying the name property in the pubspec.yaml at the root of the project (see here for more details on the pubspec).
You can probably rename your package name with the help of some dependency that you can find on pub.dev by typing "rename" in the search bar.
All those dependencies will help you rename your package to a lowercase only package name, then you will be able to use the flutter create . command to create the web folder for your app.

dart reflectable in library

I'm making a library for persistence implementation. For that I want to use reflection to be able to restore an object from the storage.
I can't use dart:mirrors because this library will be use also in Flutter application. So I'm trying to use reflectable package. And here I'm getting a major issue with understanding of how it should work. The doc tells reflectable uses build package and that some files must be generated. But I couldn't find anywhere whether this generation should happen for each file in my library package or just those where reflectable is used.
My library project structure is standard:
/examples
/lib
/lib/src
/lib/my_package.dart
/test
/pubspec.yaml
When I run dart run build_runner or dart run build_runner lib from the project root I see for each file in /example and /tests a matching file is generated. But in /lib folder nothing is generated. Only file where I'm intending to use reflectable is /lib/src/persistence/persistence_model.dart. I've created a file /lib/build.yaml of following content:
targets:
$default:
builders:
reflectable:
generate_for:
- src/persistence/persistence_model.dart
options:
formatted: true
but it seems to have no effect.
So, what should be right approach?
And probably a side question or rather consideration: isn't using reflection in Dart and Flutter excessively complex comparing to other languages (Python, C#, Java)?

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

Add plain Android library to a Flutter plugin

This might be a duplicate of this question. But it has no answer and I will give some details here.
I have created a module with File->New Module->Android Library inside my Flutter plugin's android project. And now I have a structure like below:
|-my_plugin
|-android
|-settings.gradle
|-build.gradle
|-mylibrary
|-build.gradle
/android/settings.gradle:
rootProject.name = 'my_plugin'
include ':mylibrary'
/android/build.gradle:
...
dependencies {
implementation project(':mylibrary')
}
When I build example plugin project (which is automatically created by Flutter CLI) with flutter build apk or flutter run, I get this error:
Project with path ':mylibrary' could not be found in project ':my_plugin'.
Any suggestions?
For those who are still looking for a solution. You can try this.
Instead of putting the below code in the plugin setting.gradle, place it in example/android/settings.gradle
include ':mylibrary'

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