A basic Dart program
The following code uses many of Dart’s most basic features:
// Define a function.
printInteger(int aNumber) {
print('The number is $aNumber.'); // Print to console.
}
// This is where the app starts executing.
main() {
var number = 42; // Declare and initialize a variable.
printInteger(number); // Call a function.
}
Here’s what this program uses that applies to all (or almost all) Dartapps:
// This is a comment.
- A single-line comment.Dart also supports multi-line and document comments.For details, see Comments.
int
- A type. Some of the other built-in typesare
String
,List
, andbool
. 42
- A number literal. Number literals are a kind of compile-time constant.
print()
- A handy way to display output.
'…'
(or"…"
)- A string literal.
$variableName
(or${expression}
)- String interpolation: including a variable or expression’s stringequivalent inside of a string literal. For more information, seeStrings.
main()
- The special, required, top-level function where app executionstarts. For more information, seeThe main() function.
var
- A way to declare a variable without specifying its type.
Note: This site’s code follows the conventions in the Dart style guide.