How to run Pub.build() in Dart 2 - dart

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

Related

"dart: Command not found" when running Dart commands in Codemagic

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

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 can I debug Dart Builders with breakpoints in WebStorm

Dart uses the build package to generate code. What if you want to debug your own Builder or an existing one?
Instead of calling pub run build_runner you can execute and debug the file build.dart which can be found here:
.dart_tool/build/entrypoint/
To enter your command like serve as in pub run build_runner serve you can edit your debug configurations:
Put the command inside the field Program arguments:
Click Apply and start debugging again. Webstorm will interrupt when a breakpoint is reached.

Build flutter app for desktops

I saw a few peoples managed to build flutter apps for other OS than the usual Android/IOS
My question here is simple : How ? What is the current process to build a flutter app for mac/windows ? There's no need for it to be production ready. Something experimental is enough
For those wondering how to :
https://github.com/google/flutter-desktop-embedding
There's an example using openGL to render a flutter app
Run a Flutter project in Desktop
Step 1:
For Flutter to run on Desktop, we must be on the master channel, with the latest release. So run from cmd,
flutter channel master
and
flutter upgrade
Step 2:
Then we have to enable flutter desktop support.
set ENABLE_FLUTTER_DESKTOP=true
Step 3:
Then clone this repo and cd example directory.
Step 4:
Then replace the lib folder inside the example directory with our existing code, and replace the pubspec.yaml file, with our existing one.
Step 5:
Then run from terminal
flutter packages get
and
flutter run
You can find more info here.
You can check this link out
https://github.com/google/flutter-desktop-embedding
Still not stable but does a good job of rendering flutter apps on desktop
Here's something I found useful, it is currently in alpha version but does the job by enabling us to develop Mac and Windows apps in Flutter :
https://feather-apps.com/
For those who want to know the current state(2021), here is the startup project to help you test It for MacOS, Linux, Windows. The project heavily modified from official ci to build cross-platform easily. You might want to check the ci.yml If you want to build on certain platform without github-action.
In additional, go-flutter is also a valid option, that used go-lang and openGL to achieve cross-platform features.
If you want to know the difference between official and go-flutter, here is the issue about the details.

Dart analyzer is not parsing the dynamic generated files

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

Resources