Dart uses camelCase by convention. Variables are statically typed with type inference, and the language enforces null safety by default.
Comments
// Single-line comment
/* Multi-line comment */
/// Documentation comment (used by dart doc)
Variables
// Type inference with var
var name = 'Ada'; // String
var age = 30; // int
var price = 9.99; // double
var active = true; // bool
// Explicit types
String name = 'Ada';
int age = 30;
double price = 9.99;
bool active = true;
final and const
// final — set once at runtime
final String name = 'Ada';
final now = DateTime.now(); // ok — runtime value
// const — compile-time constant
const pi = 3.14159;
const defaultTimeout = Duration(seconds: 30);
// const now = DateTime.now(); // error — not compile-time
dynamic and Object?
dynamic value = 'hello'; // disables static type checking
value = 42; // ok
Object? maybeNull = null; // nullable Object
Strings
var greeting = 'Hello';
var name = 'Dart';
// String interpolation
var message = '$greeting, $name!'; // Hello, Dart!
var lengthMsg = 'Name has ${name.length} characters';
// Multiline
var multiline = '''
Line 1
Line 2
Line 3
''';
// Raw strings
var raw = r'C:\Users\name'; // no escape processing
Numbers
int count = 42;
double price = 9.99;
// Parsing
int.parse('42'); // 42
double.parse('3.14'); // 3.14
// Converting
42.toString(); // '42'
3.14.toStringAsFixed(1); // '3.1'
Functions
// Basic function
String greet(String name) {
return 'Hello, $name';
}
// Arrow syntax for single expressions
String greetShort(String name) => 'Hello, $name';
// Optional positional parameters
String makeGreeting(String name, [String? title]) {
return title != null ? '$title $name' : name;
}
// Named parameters
void createUser({required String name, int age = 0}) {
print('$name, $age');
}
// Calling with named params
createUser(name: 'Ada', age: 30);
Cascade notation
var paint = Paint()
..color = Colors.black
..strokeCap = StrokeCap.round
..strokeWidth = 5.0;
Next: Conditions & Switches