课程: Introduction to Dart

Main entry point

- [Instructor] Every house has at least one entry and exit door for easy access. These doors are an important part of the house because they provide access and security to the occupants. Similarly, every application has a point in its program that serves us an entry point for the application. When you run an application, it starts from the specified point. Dart entry point is represented by the function main. If you don't know what a function is yet, that's okay. We will go over functions in chapter five. This function must be included in the file that sets as your programs entry point because it tells Dart where to start the program. This is usually stored in a file called main.dart. In this course, we will use DartPad instead of creating Dart files in a traditional integrated development environment, IDE. This will save us the hassle of using an IDE. DartPad automatically includes a main function in the environment so you don't have to create one yourself. So what happens if your program's main function isn't declared? Dart throws an error and your application will not run. The code in the main function is surrounded by the curly braces. Let's start with the basic syntax. The keyword main is followed by a pair of parenthesis and open curly bracket. To start writing your code, begin on the next line and end with the closing curly bracket on the line after the last line on your code. Let's look at how the main function is the Dart program's entry point. In this example, I've written a main function that displays the message, "Hello World" in the console. Let's run the code to see what happens. (mouse clicks) Now that you are familiar with the main function and its importance when writing Dart code, you will be able to properly configure your main function for your future Dart projects.

内容