Can't run Dart 2 command line programs - dart

I am trying to write a simple command line program with Dart 2. I have used Dart 2 before with Flutter without any problems, but when trying to run my command line application, I can't get it to work.
I am getting the following error:
NoSuchMethodError: Attempted to use type '_Testtt' as a function. Since types do not define a method 'call', this is not possible. Did you intend to call the _Testtt constructor and forget the 'new' operator?
Receiver: _Testtt
I am confident that nothing is wrong with my class. Also, in VS Code, it recognizes that it is in fact a constructor.
I am using the Dart version that ships with Flutter.
Does any one have an idea?
Dart Version:
Dart VM version: 2.0.0-dev.59.0.flutter-ff815d05a5 (Tue May 29 20:01:09 2018 +0000) on "windows_x64"
pubspec:
environment:
sdk: '>=2.0.0-dev.55.0 <2.0.0'
dependencies:
http: "^0.11.3+16"
dev_dependencies:
test: ^0.12.30

Don't use the modified flutter SDK for normal Dart programs. Download the regular SDK (albeit the dev channel - currently 2.0.0-dev.60.0) and install it somewhere away from your flutter installation. Use this SDK for non-Flutter Dart programs.
There's a command line argument that you need to turn on to get the regular Dart SDK to support the new 2.0 features like optional new. When running from the command line specify --preview-dart-2.

Related

dart SDK version doesn't seem to change

I had Dart SDK with version 2.16 installed and worked fine on my machine.
I tried to install the Dart with version 3 which is currently in development (3.0.0-0.0.dev), so in order to try it, I replaced the path of the Dart sdk in the environment variables with the path of the new dart with version 3, Then I created a new project on VScode, and this showed up:
Here I am writing code with the Records feature ( Which exists only in that Dart 3 dev version), and this error shows up:
This requires the 'records' language feature to be enabled.
Try updating your pubspec.yaml to set the minimum SDK constraint to 3.0.0 or higher, and running 'pub get'.
But on my pubspec.yaml:
environment:
sdk: '>=3.0.0-0.0.dev <3.0.0'
but that error is thrown, and I have no idea what cause it and how I can fix it and run the code successfully.
Thank you.

Having problems with imports in dart 2.17 in vscode

I'm having problems with VS Code not able to find packages after updating to dart 2.17. This worked fine while using dart 2.16.
With the following dart file:
import 'package:test/test.dart';
...
And the following pubspec.yaml
dev_dependencies:
test: ^1.17.4
VSCode is showing that there's a problem:
Target of URI doesn't exist: 'package:test/test.dart'.
Try creating the file referenced by the URI, or Try using a URI for a file that does exist.
The project can be run from CLI fine, just that VS Code IDE is showing issues to resolve.
This is using:
VSCode Version: 1.67.2
Dart extension in VSCode: v3.40.0
macOS 12.4
Dart SDK version: 2.17.1 (stable) (Tue May 17 17:58:21 2022 +0000) on "macos_arm64"
Note: This is a pure dart project, no flutter libraries are installed/used.

VS Code-'dart' is not recognized as an internal or external command, operable program or batch file

I am trying to install flutter and run the basic flutter example on visual studio code . I used flutter doctor in the command line and installed all the missing requirements , now i try to run a basic program in flutter ,but in visual studio code output i get the following error
[Running] dart "c:\fluttercode\mobile_test1\lib\main.dart"
'dart' is not recognized as an internal or external command,
operable program or batch file.
[Done] exited with code=1 in 64.965 seconds
I could find only one question similar to this in stack overflow ,
Visual Studio Code: dart not found
they told to change environment variables ,i added the environment variables (however the name of the environment variables was not mentioned in the answer )
I checked the dart site in case they mentioned it ,but i couldn't find anything related to the name of the environment variable
This is what i got from dart site
Windows:
In Windows, setting your PATH and environment variables can be done by opening Control Panel and typing “Environment” and then clicking the “Edit the system environment variables” option. You’ll be presented with a list of environment variables including PATH where you can edit or add new ones.
Note: You should reboot your computer after making changes to PATH to ensure all processes (including explorer) are updated.
So i just did a guess and added an environment variable with the name FLUTTER_ROOT(cause i remember reading somewhere in one of the settings in VS Code for dart ,this name was used) and pointed it to my flutter bin directory
I restarted my PC and opened vs code and changed in VS Settings >user settings>dart and flutter>Dart:Env and updated my path
{
"files.autoSave": "afterDelay",
"dart.flutterSdkPath": "C:\\src\\flutter\\bin"
}
But i get the same error
this is how my flutter bin folder looks like
Environment variables in PATH after installing Dart
You don't have to install the dart SDK if you already have the flutter SDK installed. When you specify the path of flutter, VSCode only recognizes the flutter commands and not the dart commands. What you should do is change the system environment variables (type env in the windows search bar) and add the dart sdk inside the PATH variable. The dart sdk is usually found inside /bin/cache/dart/bin. This worked for me, so give it a try
Try to add the flutter\bin sdk path and also the windows\system32 path to the user variables and/or system variables.
I had the same problem. Here is how I resolved it.
Download and extract dart sdk (I used the stable release)
https://v1-dartlang-org.firebaseapp.com/install/archive
add the dart sdk bin folder to your path
after you have added it to your path run
flutter pub global activate webdev
now i can run
webdev serve
without error.
I'm using windows so I used choco install dart-sdk -<version> to install dart through the powershell in admin mode. That got rid of my error
Installing flutter is not enough. You need to install the dart sdk.
Pre-req: Make sure you have Chocolatey installed on your machine.
Run "choco install dart-sdk" from an elevated prompt.
Restart vscode.
That did it for me.
I went to the dart website and searched for the dart sdk.
i then chose the option to install dart using setup wizard.
After running the executable file on my laptop it took some time to download all the files but once the setup was complete the error no longer occurred and i was able to run the code successfully.
Downloading dart setup from dart website
Installing using setup wizard

Decompiling .dart.snapshot into Dart source code

According to dart-lang/sdk:
Starting in 1.21, the Dart VM also supports application snapshots, which include all the parsed classes and compiled code generated during a training run of a program.
$ dart --snapshot=hello.dart.snapshot --snapshot-kind=app-jit hello.dart arguments-for-training
Hello, world!
$ dart hello.dart.snapshot arguments-for-use
Hello, world!
Now,how can i decompile this hello.dart.snapshot file to hello.dart?
In android Apk that written by java language we can decompile apk and get jar file from class.dex using dex2jar tools, but when application developed by flutter framework(written with dart)how can decompile this application and get application dart classes?
This image show snapshot files that generated in apk assets file.
In release mode, Flutter compiles the Dart code to machine code, currently only ARMv7 (this procedure is called AOT - Ahead-Of-Time compilation). Unlike native Android apps, in which the Java is compiled to byte-code named Smali, which can be (pretty easily) decompiled to Java back again.
Most of the machine code is compiled to the file "isolate_snapshot_instr", which is written in a special format, and the flutter engine (flutterlib.so, also found inside the app), loads it into the app memory in run time. Therefore, you have 2 reasonable options:
Reading the app code at runtime (the .text segment). You can use
frida dump for that, and extract the compiled Dart code that
you need
Pacthing/Using the Flutter engine in order to deserialize the machine code
If you have ipa (IOS app), that could be easier, because all of the code is found in App.Framework.

how do you set your version of dart via pub?

I wasnt sure if i need to update it through their website or something, or if it is possible do call some sort of dart version command which will update DART to be a particular version.
Example: Dart is either 1.14, or 1.16. If i want to do it through pub, is there a way to do something like: pub set dart 1.15 or something. I know set isnt a valid command, but i think you get the jist.
This depends on your operating system and what system you use to install Dart.
For Debian you can use apt-get install dart. apt-get allows to pass a specific version number.
For installation options see https://www.dartlang.org/downloads/
In pubspec.yaml you can only specify what DART SDK version and dependency versions are supported by a package but it doesn't have and influence which Dart SDK version gets installed.

Resources