Dart_LoadScriptFromKernel: The binary program does not contain 'main'

Viewed 8484

I just run this code in VsCode and show this problem

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

Dart version:2.3.2

void main() {
  print("hello");
}

is there any wrong with configuration? screenshot

7 Answers

try saving it in your folder it works 100% and name it main

In Visual Studio Code, this error happens when you are trying to run your main.dart file, but you have another window (or tab) open with a class file for example. Make sure when you run your code you have the main (main.dart) tab open with your main function in it.

  1. On Visual Studio Click on "View"
  2. Then click on Run
  3. Then on the left top corner see run drop-down menu
  4. select add configuration.
  5. Then a JSON file appears.
  6. Now delete everything and paste the below:
 {    
     "version": "0.2.0",
     "configurations": [
        {
            "name": "Dart: Attach to Process",
            "type": "dart",
            "request": "attach"
        },
        {
            "name": "Dart",
            "type": "dart",
            "request": "launch",
            "program": "bin/main.dart"
        },
        {
            "name": "Dart: Run all Tests",
            "type": "dart",
            "request": "launch",
            "program": "./test/"
        },
        {
            "name": "Dart: Attach to Process",
            "type": "dart",
            "request": "attach"
        },

        {
            "name": "Dart",
            "program": "bin/main.dart",
            "request": "launch",
            "type": "dart"
        }
    ] }
  1. Now again go to let hand side top and see beside run there is a play type button.
  2. Just press it and allow permission

  3. Now run the code in the terminal. It will work.

I also faced the same problem. Then I found that I did not save the file (code). Then I save it (ctrl+s or File > Save). Finally I run the command

dart hello.dart 

and get the expected output.

VScode always requires a main.dart file in the project directory with at least the minimum contents:

void main() {}

With that file present, you can then name any other file you're working on, and execute it directly from the VScode console using eg. dart hello_world.dart (with example contents):

void main() {
  print("Hello World");
}

and the main.dart file does not need to be open when this is done.

you have to do is just save the file ctrl+s

Related