Dart Important Concepts
1. Anything placed in a variable is an object, and an object is an instance of a class.
Numbers, functions, and null are all objects in Dart, and all these objects inherit from the Object class except null (if sound null safety is enabled).
2. Dart 2.12 supports null safety, which states that the code is non-nullable by default. This means that the variables can't contain null value unless we explicitly mention it using a question mark '?' at the end of its data type. With this feature, runtime null-dereference errors turn into edit-time analysis errors.
If you are sure that an expression can never evaluate to null, but Dart disagrees, you can add '!' to assert that it isn't null.
3. Type annotations are optional in Dart, although it is a strongly typed language. Dart is capable of inferring types on its own by scanning the value assigned to the variable.
4. Dart supports generic types as a list of integers like, List <int> or a list of objects, like List<Object>.
5. Dart supports the following functions :
- Top-level functions like main( ) which is the entry point of the program.
- Static Methods are the functions tied to a class.
- Instance Methods are the functions tied to an object.
- Nested or Local functions are the functions listed within the functions.
6. Similarly, Dart supports the following variables :
- Top-level variables
- Static variables are the variables tied to a class.
- Instance variables are the variables tied to an object.
7. Unlike Java or C++, Dart doesn't have access specifiers like private, public, or protected to limit the accessibility of members of a class.
To make a variable private, we can use underscore '_' before it.
For example, int _a;
In Dart, privacy is at the library level which means that all the other classes and functions within that library can access those members. So, in Dart we mainly have public or private accessibility.
8. Dart has expressions which have runtime values and as well as statements which don't have runtime values.
For example, the conditional expression,
condition ? expression1 : expression2 has a value of expression 1 if at runtime, condition evaluates to True or the value of expression 2 if the condition evaluates to False. In contrast, an if-else statement doesn't have any value.
9. Dart tools report two kinds of problems :
- Warnings are the indications given by the compiler that the code might not work, but they don't prevent the program's execution.
- Errors are the problems in the code that prevent its execution. They are of two types :
- Compile-time error = This error prevents the code from executing at all.
- Runtime error = This error results in an exception raised during code execution.
10. The Dart virtual machine ( VM ) is the core of the Dart language. It is mainly required when we are compiling our code using command line VM on the console. Another use is embedding it in another application, such as Dartium.
11. Dartium is a customized build of Chromium with the Dart VM embedded in it. It executes the Dart code natively in the browser without converting it to JavaScript.