So I have received a project which requires e-commerce, food ordering and car hailing services like Grab to be inside one app. I have delivered all these 3 apps in different projects before and for this new project I'm wondering whether it is possible to combine all my previous source code into one project, running on different dependencies.
You can create packages for all 3 modules (food, e-commerce, car-hailing services)
To create a Flutter package, use the --template=package flag with flutter create:
flutter create --template=package food
Folder Structure
lib
packages
- food
- e_commerce
- car_hailing_service
Import this in the main module's pubspec.yaml file
dependencies:
flutter:
sdk: flutter
# My Custom Packages
food:
path: packages/food
e_commerce:
path: packages/e_commerce
car_hailing_service:
path: packages/car_hailing_service
If you have any common functionalities for all 3 packages then create a separate package for that also and import that common package for all 3 packages.
Refer to this document for creating flutter packages. I hope this helps you.
Related
I'm working on one project for some time now on flutter. Part of the source code has been designed so that it can be used again as is in other projects.
I'm working with Visual Studio Code.
Now I'm creating a second project. I'd like to organize folders this way:
Parent folder
Project1 folder
Project2 folder
my_library
Is it possible to add the library folder to the projects, as it is not inside their respective folders?
In pubspec.yaml of project 1, refer to the library as:
dependencies:
my_library:
path: ../my_library
The way to solve this isn't straightforward for beginners. So I summed up the proposed solutions here (I provide the names out of fairness, and follow related potential discussions below each one).
From Richard Heap:
In pubspec.yaml of project 1, refer to the library as:
dependencies:
my_library:
path: ../my_library
From me:
In ../my_library, add a specific pubspec.yaml. Something like:
name: my_libraries
description: my own common libraries
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ">=2.17.1 <3.0.0"
dependencies:
flutter:
sdk: flutter
From me:
Drag and drop (maybe there's a menu) your my_libraries folder from a file explorer to the VSCODE Explorer panel, and choose "add folder to workspace". Of course, all dependencies to external libraries used in my_libraries must be specified in its own pub_spec.yaml file.
Remarks:
I added source files in my_libraries/lib/. Don't know if the lib sub-directory is mandatory. Didn't take time to test without, and I like better to keep the same structure in my_libraries than in projects.
in project (and my_library) source files, to import my_library source files, just do as:
import 'package/my_library/xxx.dart'
where xxx.dart is the file to import.
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)?
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
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';
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