advertisement
Understanding Basics of Dart Programming
Basic syntax in a Dart program comprises several elements such as keywords, data types, variables, constants, string literals, and symbols. These are used in statements and expressions within Dart programs. Variables are used to hold data belonging to different data types.
This is a simple example of defining variables for a number and a string respectively.
The main () function is always predefined in Dart. The main () method acts as an entry point into a Dart application. A variable x is declared and assigned the data type "int". Another variable y is also declared and assigned as a 'string'. Similarly, it is possible to declare and assign other variables of different data types. After declaring the variables, they are assigned values through assignment statements. Here, x being an integer is given a value of 5, whereas, y is assigned a string literal value of Daniel.
Comments in Dart: A comment is a code that is not readable by the compiler. It makes it easier to understand the code and flow of the program. Comments can also be used to inform other developers who are collaborating on the same code about the tasks being done.
Identifiers in Dart
Identifier is the generic name for elements such as variables, functions, arrays, lists, and so on. There is a certain set of rules which have to be followed for naming an identifier.
- Valid Identifiers: Elements that follow the set of rules for naming an identifier are valid identifiers.
- Invalid Identifiers: Elements that fail to follow the set of rules for naming an identifier are invalid identifiers.
The rules for naming an identifier are as follows:
Do's:
- An identifier can have a set of characters
- An identifier can start with an alphabet
- An identifier can start with an underscore
- An identifier can also have $ and _as special characters
Don't's:
- An identifier cannot start with a digit
- An identifier cannot contain any spaces
- An identifier cannot have keywords
- An identifier cannot be duplicated
Few examples of valid and invalid identifiers.
Keywords in Dart
Keywords in Dart are reserved words that the compiler interprets specially. Each keyword has a unique connotation and purpose in the Dart programming language. Keywords are case-sensitive and cannot be used for naming variables, functions, and classes.
The following are the most important keywords in Dart:
Create a simple Hello World Dart Application
Consider an example to understand the creation of a simple Dart application.
1. Creating and Configuring the Application
A developer can perform the following steps:
- Step 1: Using File Explorer in Windows, create a folder called DarthelloWorldProgram in a suitable location on the system.
- Step 2: Launch the Android Studio application.
- Step 3: Click File and open the previously created folder inside Android Studio. Observe the autogenerated files when the folder is opened in Android Studio.
- Step 4: Right-click the project and then click New → File. Name the file HelloWorld. Dart.
The file will be created. As of now, this file type is not recognized by Android Studio. - Step 5: Click on File and choose Settings. Then, click Language and Frameworks from the side menu and select Dart. Once the side menu option has been clicked, select the checkbox that says Enable Dart support for the project DartHelloWorldProgram.
- Step 6: Copy the path where the Dart SDK is saved on your system and paste the SDK path into the Dart SDK path box in Android Studio.
- Step 7: The next step is to click Add/Edit Configuration in Android Studio and expand Defaults.
- Step 8: Then, choose Dart Command Line App from the Defaults.
- Step 9: Finally, choose the path for the Dart file and uncheck the check box that says "Checked mode" If it is checked by default then click Apply.
2. Added the main () Method
The main() function is the most important function and is considered the entry point for any Dart application. It is a preset function that is in charge of executing any Dart application. It also takes responsibility for executing all functions including predefined library functions and functions that are defined by the user. A Dart application without the main () function is impossible to execute. The void () in the main() function simply means that it returns void. It can also have an optional List<string> parameter.
In the current example, the print () function can be used in code to print a string to the console or terminal.
To add the main () function to the application, perform the following steps:
- Step 1: Go to the HelloWorld.dart file.
- Step 2: Create a main () function and add the code to print 'Hello World' inside the main function.
- Step 3: Save the program.
3. Executing the Application
To execute the application, the developer has to right-click the HelloWorld.dart file and choose Run HelloWorld.dart.
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0