I am reading documentation of the Dart's pub run command and it specifies the following syntax with a colon after the package name to run the script from the dependency package:
pub run foo:bar arg
But I remember (and double-checked it) that for code generation with build_runner I always did this, without a colon:
flutter pub run build_runner build
Moreover, if I try to run the build_runner build with a colon syntax like
flutter pub run build_runner: build
then it gives me an error: Could not find bin/.dart in package build_runner.
Why is that?
Related
when I try to run Dart commands (even simple dart --version) in Codemagic it fails with an error saying dart: command not found.
to overcome the issue you need to update your scripts to run $FLUTTER_ROOT/bin/dart instead.
There is no separate Dart SDK on Codemagic build machines, only Flutter SDK and you can use the Dart version shipped with Flutter.
For instance
#!/bin/sh
$FLUTTER_ROOT/bin/dart --version
I created a new Dart app like this:
dart create hello
I can run the app like this:
dart run hello/bin/hello.dart
I try to activate the app like this:
dart pub global activate --source path hello
But I can't run the file like I would expect:
hello
zsh: command not found: hello
The .pub-cache/bin folder is in my cache but pub global activate didn't put it there.
This does work:
dart pub global run hello
Hello world!
But I'd like to be able to run the script without typing dart pub global run every time.
If I do a package from pub.dev it works ok:
dart pub global activate webdev
It puts a webdev executable inside the .pub-cache/bin folder and I can run it.
webdev --version
2.7.4
So is there another step I need to do to make my hello app get into the executable folder?
I also tried compiling it:
dart compile exe hello/bin/hello.dart
And activating it again:
dart pub global activate --source path hello
But there is still no binary in the .pub-cache/bin folder. Any suggestions?
Update
After getting Kevin's answer below, I added the following to pubspec.yaml:
executables:
hello:
Then I ran the following command:
dart pub global activate --source path hello
which gave the following result (username modified):
Resolving dependencies...
Got dependencies!
Package hello is currently active at path "/Users/suragch/Dev/DartProjects/hello".
Installed executable hello.
Activated hello 1.0.0 at path "/Users/suragch/Dev/DartProjects/hello".
But if I run this:
hello
I get the following error:
/Users/suragch/.pub-cache/bin/hello: line 7: pub: command not found
Running this still works, though:
dart pub global run hello
Hello world!
You need to list the files under bin/ that you want to be executable in the executables section in pubspec.yaml file. For example for bin/hello.dart to be executable add the following to pubspec.yaml
....
executables:
hello:
....
Then when you run: dart pub global activate --source path hello
You can now call hello in bin/ without running: pub global run hello
Again
You have to list every executables you intend to be made available to the user of your package in the CLI under the executables key in pubspec.yaml
So it looks like this problem was caused because because of a bug as a result of switching from pub to dart pub. If you open the .pub-cache/bin/hello file with an editor, you'll see the following:
#!/usr/bin/env sh
# This file was created by pub v2.13.1.
# Package: hello
# Version: 1.0.0
# Executable: hello
# Script: hello
pub global run hello:hello "$#"
The error about line 7 is the last line where you can see it is referencing plain pub. Change that line to the following:
dart pub global run hello:hello "$#"
Now you can run your app from anywhere:
hello
Hello world!
This is only a temporary workaround. Follow this issue for updates. Thank you to Kelvin Omereshone for pointing me in the right direction.
In Dart 1.x, there was a command, that you could trigger e.g. from grinder.dart. But this line works no longer:
Pub.build();
How can I replace it and run build command of my web app from Dart code?
pub build is no longer supported in Dart 2.
You need to run pub run build_runner build.
See https://webdev.dartlang.org/dart-2
I'm not sure if there are new APIs for Grinder, though.
I fixed it by calling webdev build directly
I'm trying to get started and setup to develop Dart.
I'm following the instructions on https://webdev.dartlang.org/guides/get-started to install on Linux. However there seems to be an essential step missing.
On the above link, step 2 is about installing the SDK.
Step 3 is titled:
Get CLI tools or WebStorm (or both)
However step gives examples of using the pub command, and the links it references also use this command, however there are no instructions on where to find the pub utility or how to install it.
I would have assumed that pub was provided as part of the Dart SDK. I can run dart in my terminal, and see that it is installed. For example dart --version returns Dart VM version: 2.0.0 (Unknown timestamp) on "linux_x64". But pub returns zsh: command not found: pub
My question, therefore, is where do I find, and how do I install pub?
You need to add thedart-sdk/bin directory to the system PATH variable.
I checked and the file is OK.
Dart 1.23.0
AngularDart 3.1.0
The error I am getting is:
Target of URI hasn't been generated: 'file.g.dart'
It may help to run "Synchronize" -- bound by default to Ctrl-Q.
You can restart the analyzer on the dart analysis tab, in the top left is a button with arrows running in a circle, if you hover over it it says 'Restart Dart Analysis Server'.
Example picture
first check this article :
https://flutter.dev/docs/development/data-and-backend/json
and make sure of all steps :
1- add dependencies in pubspec.yaml
2- flutter pub get
3- flutter pub upgrade
4- flutter pub run build_runner build
after all, consider that:
part 'timeline.g.dart';
most be your file name which classes are in!
if your file name is difference with your class name, you should use your file name.
Had the same problem. Fixed it following these steps:
$ flutter pub get
$ flutter pub upgrade
$ flutter pub run build_runner build --delete-conflicting-outputs
Check have you created a constructor for class with no default parameters. Basically automated file is generated but error happens because it can not find a contractor for model
flutter pub run build_runner build
Just run the above command in the console. Everything will be fine