How to create and run a Dart project on the command line - dart

I wanted to create a Dart project to play around with, but I couldn't figure out how to create one from the command line.
I tried
dart create playground.dart
I also tried
dart playground.dart
but the code I pasted in had dependency requirements.

Update
Dart now supports creating projects directly. No more need for stagehand.
dart create playground
This creates a folder named playground with a Dart project inside.
Alternate answer
1. Create a new folder
mkdir playground
2. Add a dart file with a main() function
playground.dart
void main() {
print("hello world");
}
3. Run the app
dart playground.dart
If your app has dependencies...
4. Setup configuration
Create a pubspec.yaml file
touch pubspec.yaml
Paste in any dependencies that you have
name: playground
description: Just a place to practice
version: 0.0.1
dependencies:
crypto: ^2.0.6
5. Get dependencies
pub get

The easiest way to create a new project is to use Stagehand. This is the same that IntelliJ uses when creating a new Dart project.
You can install it with this command
pub global activate stagehand
And then all you have to do to use it is
mkdir playground
cd playground
stagehand <generator-name>
These are the different generator you can use right now:
console-full - A command-line application sample.
package-simple - A starting point for Dart libraries or applications.
server-shelf - A web server built using the shelf package.
web-angular - A web app with material design components.
web-simple - A web app that uses only core Dart libraries.
web-stagexl - A starting point for 2D animation and games.

Related

How to create pubspec.yaml by command?

I am learning Dart (2.9.3). I use pub global activate webdev command.
I think it will work like npm in Node.js and create pubspec.yaml automatically, but it didn't.
Do I need to add more switches (like --save) to pub global?
Thank you.
The pub global activate webdev command activates webdev package to use on your computer. This package is
A command-line tool for developing and deploying web applications with Dart.
As author wrote in usage section
webdev provides two commands: serve and build.
But neither of both creates pubspec.yaml file.
If you want to generate a project, you can use stagehand. But there is no option to generate customized pubspec.yaml file. Dart must know which packages to use in a particular project, and only this file can provide those informations.

how to use local flutter package in another flutter application?

How to use local flutter package in another flutter application?
I created a package using following command:
flutter create --template=package my_new_package
and then in my application source code => main.dart
import "package:my_new_package/my_new_package.dart" // can not find the package
Find this file in your flutter application => pubspec.yaml
Use local dependency
dependencies:
flutter:
sdk: flutter
my_new_package:
path: ./my_new_package
Note: The ./my_new_package above means that the my_new_package directory containing the pubspec.yaml for the package is a sub-directory of the app.
If you have the package as a directory at the same level as the app, in other words one level higher up in the directory tree, you can use ../my_new_package (note the double dot) or a full path to the package directory.
Path dependency: A Flutter app can depend on a plugin via a file system path: dependency. The path can be either relative or absolute. For example, to depend on a plugin plugin1 located in a directory next to the app, use the following syntax:
dependencies:
plugin1:
path: ../your_package/
For the full process:
Download the code for the plugin you want to use and place it at the "same" level as your flutter project directory
-- plugin-name
-- your flutter directory -- lib
-- android
-- ios etc etc
Add the plugin path to pubspec.yaml. *If you are unsure of the correct plugin name to use, look at the name: attribute in the plugin's pubspec.yaml file. The plugin directory must also be saved with the same name:
dependencies:
plugin-name:
path: ../plugin-name
Run Pub get and you can import just like any other plugin. Only difference is when you click on any of the plugin classes during development, it will point to the local file.
Hey I had same problem once I started using flutter.
I have implemented example of pdf_view plugin in my app for making changes for Shimmer effect instead of CircularProgressIndicator().
Some Knowledge
You can edit plugins which are get by flutter pub get but they might be replaced when you create app bundle by flutter.
Now your answer with sample plugin suppose take an example of advance_pdf_viewer
GitHub Link
Download zip file and extract it in pdf_viewer/
Make sure pdf_viewer/ has all files including lib, Android, iOS and all related files.
Copy your pdf_viewer folder in your project directory for example
my project is invoice_viewer so it's root directory have all folders such as lib, Android, iOS etc. copy it in this root directory along with lib, Android, iOS.
Now open your pubsec.yaml and write code as follows
dependencies:
flutter:
sdk: flutter
# advance_pdf_viewer: ^2.0.0
advance_pdf_viewer:
path: ./pdf_viewer
In comment I have replaced server version with local one and make changes in plugin's viewer.dart file to achieve desired changes.
Hope you and other got some information from this finding!

NPM 'prestart' script equivalent in Flutter

In a package.json file (which is basically the Node version of Flutters pubspec.yaml) You have a scripts section where you can add your own custom scripts. Normally with a node project you'll have a start script that will kick off the build and, well, start the project. In flutter you have flutter run.
In my flutter project I'm using the json_serializable package that generates the code that I use when serialising my objects to JSON. Right now I have to have two terminal windows open:
Tab 1
Runs flutter packages pub run build_runner watch that does the code generation and watches the file system.
Tab 2
Run flutter run that runs the project with hot reloading.
So it would be great if you had something like Nodes prestart in pubspec.yaml where I can run the code generation automatically when I run flutter run
There currently is no such thing in Dart. You can create your own Dart or shell scripts in tool/ that runs your builder_runner command detached (in the background) and the flutter run command in the foreground.
you can start this just using tool/run.dart
However with build becoming mature and pub serve/pub build being deprecated I assume the Dart team is thinking already about making this a more pleasant experience.

How to run angulardart application?

I just download andgularjs dart tutorial from angulardart.org . It provides me some tutorials, but I couldn't able to run those tutorials.
I am using webStorm IDE to run those application. when I am trying to run the application, I am getting following error.
enter image description here
I couldn't find package folder with in web root folder in that application.
You probably need to run pub upgrade. WebStorm provides this action in the context menu of the pubspec.yaml file, also if you open the pubspec.yaml file in the WebStorm editor links for pub actions are shown in the top-left corner of the editor. Alternatively you can run pub run from the command line of your project directory if the path is configured properly on your system so the command is found.

Dart Package Management via dart2js

I'm learning Dart and its dependency manager pub and am having a tough time seeing the "forest through the trees" here.
Say I want to use Polymer.dart in my project. So, in my project root, I create the following pubspec.yaml:
name: test_dart
description: A sample web application
dependencies:
browser: any
polymer: ">=0.9.0 <0.10.0"
I then run pub get, which goes to the pub repo and fetches the browser and polymer dependencies that I've specified. It then creates a packages directory in my project root, which now means I have a project that looks like:
MyDartProject/
pubspec.yaml
myapp.dart
packages/
browser/
...
...all the packages that ship with Polymer
Now I start coding my Dart web app (myapp.dart), which will references various Polymer and browser types/functions/etc. in its source code.
When I'm all done, I want to create a JavaScript file called myapp.js.
According to the dart2js docs, I need to run something like:
dart2js --out=myapp.js --package-root=??? myapp.dart
How do I include all the browser & polymer packages on the buildpath?
There is a "pub build" option now.
http://pub.dartlang.org/doc/pub-build.html
Use pub build when you’re ready to deploy your web app. When you run
pub build, it generates the assets for the current package and all of
its dependencies, putting them into a new directory named build.
$ cd ~/dart/helloworld
$ pub build
Building helloworld......
Built 5 files!
If the build directory already exists, pub build deletes it and then creates it again.
That should do everything you are after here. You can also launch it from the IDE by right clicking on the pubspec.yaml file and choose "pub build"
EDIT: You should also see the links in zoechi's answer.
If you run dart2js from your MyDartProject directory you don't have to provide --package-root parameter.
An alternative way is running pub build. If you use Polymer you need to add a transformers section.
see also
How to deploy a Dart Polymer app to Javascript using dart2js
How do I pass multiple entry_points to polymer transformer in pubspec.yaml?

Resources