NANDHOO.

Dart Fundamentals

Welcome to your first step in becoming a Flutter developer! Before we dive into building beautiful apps, we need to master the language that powers them: Dart. Dart is a client-optimized language for fast apps on any platform. It's type-safe, easy to learn, and incredibly powerful.

Why This Topic Matters

Dart is the foundation of Flutter. Every button you press, every screen you navigate to, and every piece of data you fetch in a Flutter app is handled by Dart code. Understanding its core syntax, how it handles variables, and how to control the flow of your program is essential. Without a solid grasp of Dart, you'll find yourself struggling to understand why Flutter widgets behave the way they do. Mastering these fundamentals early will save you hours of debugging later.

How To Study This Chapter

Don't just read the code—run it! You can use DartPad to try out every example in this chapter without installing anything. Type the examples, change the values, and see what happens. Try to "break" the code and understand why it doesn't work when you make certain changes. This hands-on approach is the most effective way to internalize the syntax and behavior of Dart.

Variables and Data Types

In Dart, everything is an object. Variables store references to these objects. Dart is statically typed, meaning the type of a variable is known at compile-time. However, Dart's type inference often makes explicit type declarations optional.

Declaration Keywords

  • var: Automatically infers the type.
  • final: A variable that can be set only once.
  • const: A compile-time constant.
void main() {
  var name = 'Flutter'; // Inferred as String
  final version = 3.10; // Cannot be changed after assignment
  const pi = 3.14;     // Must be known at compile-time

  print(name);
}

Basic Types

Dart supports several fundamental data types:

  • Numbers: int (integers) and double (floating-point numbers).
  • Strings: String (UTF-16 code units).
  • Booleans: bool (true or false).
  • Lists: List (ordered group of objects, similar to arrays).
  • Maps: Map (key-value pairs).
int age = 25;
double height = 5.11;
String greeting = "Hello Dart!";
bool isAwesome = true;

List<String> platforms = ['Android', 'iOS', 'Web'];
Map<String, String> capitals = {
  'USA': 'Washington D.C.',
  'India': 'New Delhi'
};

Control Flow

Control flow statements allow you to dictate the path your program takes based on conditions and loops.

If and Else

int score = 85;

if (score >= 90) {
  print('Excellent!');
} else if (score >= 75) {
  print('Good Job!');
} else {
  print('Keep practicing!');
}

For Loops

Use loops to repeat a block of code multiple times.

// Standard for loop
for (int i = 0; i < 5; i++) {
  print('Iteration $i');
}

// For-in loop for collections
var fruits = ['Apple', 'Banana', 'Orange'];
for (var fruit in fruits) {
  print('I love $fruit');
}

While and Do-While

int count = 0;
while (count < 3) {
  print('Counting... $count');
  count++;
}

String Interpolation

Dart makes it easy to include variable values within strings using the $ symbol.

String user = 'Alice';
int points = 100;
print('User $user has $points points.');
print('Double the points: ${points * 2}'); // Use {} for expressions

In the next chapter, we'll dive deeper into Dart's object-oriented features, which are the building blocks of Flutter widgets!