Difference Between final and const in Dart

In Dart, both final and const are used to create variables whose values cannot be changed after initialization (immutable). However, there are important differences between them.

1. Time of Value Assignment

  • final → Value is determined at runtime (run-time constant). It can come from calculations or inputs known only when the program runs.
  • const → Value must be known at compile-time (compile-time constant). Cannot depend on values computed at runtime.

//  Allowed: runtime value
final currentTime = DateTime.now();

//  ERROR: must be compile-time constant
const currentTimeConst = DateTime.now();

2. Level of Immutability

  • final → The variable itself cannot be reassigned, but the content of the object it references can be changed if it's mutable.
  • const → The value and the object are deeply immutable. Const objects are also canonicalized (all const objects with the same value point to the same memory instance).

final listFinal = [1, 2, 3];
listFinal.add(4); //  Allowed, list is mutable
// listFinal = [5, 6]; //  ERROR

const listConst = [1, 2, 3];
// listConst.add(4); //  ERROR, immutable

3. Usage in Classes

  • final → Can be used for instance variables set once, often via constructor.
  • const → Only allowed for:
    • static const (class-level constants)
    • const constructors for immutable objects

class Person {
  final String name; // set via constructor
  static const maxAge = 150; // compile-time constant
  Person(this.name);
}

4. Quick Comparison Table

Aspect final const
Evaluation Time Runtime Compile-time
Mutable Object Content Yes (if object is mutable) No
Usage in Classes Instance variables Static const / const constructors
Canonicalization No Yes

5. Flowchart: When to Use final vs const

If Blogger supports Mermaid.js, you can embed this diagram:


flowchart TD
    A[Do you know the value at compile-time?] -->|Yes| B[Use const]
    A -->|No| C[Use final]
    B --> D[Value never changes & deeply immutable]
    C --> E[Value set at runtime, but variable cannot be reassigned]

If Blogger does not support Mermaid.js, here’s the simplified logic:

  • If the value is known before the program runs → Use const
  • If the value is known only when the program runs → Use final

Fix: VirtualBox can't operate in VMX root mode (VERR_VMX_IN_VMX_ROOT_MODE)

Quick guide to diagnose and fix the error when VirtualBox cannot access hardware virtualization because KVM is using VT-x/AMD‑V.

Problem

When starting a VM, VirtualBox shows the error:

VirtualBox can't operate in VMX root mode. Please disable the KVM kernel extension, recompile your kernel and reboot (VERR_VMX_IN_VMX_ROOT_MODE).
Result Code:
NS_ERROR_FAILURE (0X80004005)
Component:
ConsoleWrap
Interface:
IConsole {6ac83d89-6ee7-4e33-8ae6-b257b2e81be8}

This means VirtualBox is trying to use hardware virtualization (Intel VT-x / AMD‑V) but the host kernel already has KVM claiming the CPU virtualization features.

Common Causes

  • The KVM kernel modules (kvm_intel or kvm_amd) are loaded.
  • You recently used QEMU/KVM or your distribution auto-loads the KVM modules at boot.
  • Both KVM and VirtualBox require exclusive access to VT-x / AMD‑V, so they cannot run simultaneously in the host.

Quick (temporary) fix — unload KVM modules

Run these commands in a terminal. Use kvm_intel for Intel CPUs or kvm_amd for AMD CPUs. You may need root privileges.

sudo rmmod kvm_intel
sudo rmmod kvm_amd
sudo rmmod kvm

# Verify KVM modules are unloaded
lsmod | grep kvm

If lsmod | grep kvm returns nothing, KVM is unloaded and VirtualBox should be able to start VMs.

Permanent solution — blacklist KVM modules at boot

If you mostly use VirtualBox and rarely use KVM/QEMU, you can prevent the KVM modules from loading automatically:

sudo nano /etc/modprobe.d/blacklist-kvm.conf

Add these lines:

blacklist kvm
blacklist kvm_intel   # for Intel CPUs
blacklist kvm_amd     # for AMD CPUs

Save the file and reboot. The modules will not load automatically after boot.

Notes & alternatives

  • KVM and VirtualBox both need direct access to the CPU's virtualization features, so they cannot both be the host-level hypervisor at the same time.
  • If you need to run both technologies simultaneously, you could run VirtualBox inside a KVM virtual machine using nested virtualization, but this is more complex and may be unstable or slower.

Want a small helper script to toggle KVM on/off without rebooting? I can prepare one for you — tell me if you want an Intel or AMD version.

Control Flow in Flutter: A Fun Guide to if, switch, for, and while

When building apps with Flutter (or Dart, to be precise), you'll quickly run into scenarios where your code needs to make decisions or repeat actions. That’s where control flow comes in! Just like traffic lights direct cars, control flow directs the flow of your program  deciding what runs, when, and how often.

What is Control Flow?

Control flow is simply the order in which code is executed. Dart, like most programming languages, gives us several tools to control that flow: if statements, switch cases, and looping tools like for and while.

1. if and else

The if statement is the most basic decision-maker. Think of it as: "If this condition is true, do something." You can also use else for an alternative path.


int age = 20;

if (age >= 18) {
  print("You're an adult!");
} else {
  print("You're still a minor.");
}

You can also chain multiple conditions using else if:


int score = 85;

if (score >= 90) {
  print("A");
} else if (score >= 80) {
  print("B");
} else {
  print("C or lower");
}

2. switch

Tired of writing a ton of if-else blocks? That’s where switch shines. It’s great when you have multiple specific values to check.


String day = "Monday";

switch (day) {
  case "Monday":
    print("Ugh, it's Monday!");
    break;
  case "Friday":
    print("Yay, it's Friday!");
    break;
  default:
    print("Just another day.");
}

Note: Don't forget to use break after each case unless you want a fall-through (which Dart doesn't allow by default).

3. for loop

Need to repeat something a known number of times? Use a for loop. It's perfect for iterating through lists or counting.


for (int i = 0; i < 5; i++) {
  print("Count: $i");
}

You can also loop through lists:


List fruits = ['apple', 'banana', 'cherry'];

for (var fruit in fruits) {
  print(fruit);
}

4. while and do-while

If you want to keep looping until a condition is false (but you don't know how many times that will take), use while or do-while.


// while
int i = 0;
while (i < 3) {
  print("Hello $i");
  i++;
}

// do-while (runs at least once)
int j = 0;
do {
  print("Number $j");
  j++;
} while (j < 3);

When to Use Which?

  • Use if/else when you’re checking conditions that are boolean (true/false).
  • Use switch when you have several possible fixed values to check.
  • Use for when you know how many times to repeat.
  • Use while when you want to loop as long as something is true.

Tips & Best Practices

  • Keep your conditions simple and readable.
  • Don’t nest too many if blocks—it becomes messy fast.
  • Use meaningful variable names to make logic easier to follow.
  • Always test edge cases (e.g., empty list, null value).

Wrapping It Up

Control flow might sound like a fancy term, but it’s really just how we make our code act smart. Whether it's choosing a path with if, selecting from options with switch, or repeating actions with for and while, you now have the power to guide your Flutter app’s behavior.


Mastering Functions and Parameters in Dart for Flutter Developers

If you're diving into Flutter, sooner or later you're going to bump into functions a lot of them. From handling a simple button press to building your own custom widgets, functions are the unsung heroes behind almost everything in Dart. And guess what? They’re actually fun once you understand how they work.

What Are Functions in Dart?

In Dart, a function is a reusable block of code that performs a specific task. You can think of it like a mini-machine: you give it some input (if needed), and it gives you output (if you want). Functions help keep your code clean, organized, and easier to maintain.

Basic Function Example

void sayHello() {
  print('Hello, Flutter Devs!');
}

You can call that function by simply writing sayHello(); in your code. Super simple, right?

Functions with Parameters

Most functions are more useful when they can take in information that’s where parameters come in. You define parameters inside the parentheses when creating a function.

void greetUser(String name) {
  print('Hello, $name!');
}

Now when you call greetUser('Alice');, it will print: Hello, Alice!

Default and Optional Parameters

Dart allows you to make your parameters optional or give them default values which is super handy when you want flexibility.

Named Parameters (Optional)

void greet({String? name}) {
  print('Hello, ${name ?? 'Guest'}');
}

Named Parameters with Default Values

void greet({String name = 'Guest'}) {
  print('Hello, $name');
}

When calling these, you can do: greet(); or greet(name: 'Bob');

Functions with Return Values

Sometimes you want your function to send something back that’s called a return value.

int add(int a, int b) {
  return a + b;
}

When you call add(3, 4);, it returns 7. Simple math, but powerful when used right!

Arrow Syntax (Shorthand)

Dart also lets you write one-liner functions in a cleaner way using the => arrow syntax.

int square(int x) => x * x;

Anonymous Functions (Lambdas)

You can even define functions without giving them names. These are useful when passing logic into other functions, like callbacks or mapping over lists.

var numbers = [1, 2, 3];
numbers.forEach((n) {
  print(n);
});

Functions Are First-Class Citizens

In Dart, you can treat functions like any other variable assign them to variables, pass them around, even return them from other functions. This opens doors to functional programming techniques.

void printMessage(String message) {
  print(message);
}

void runFunction(void Function(String) fn) {
  fn('This is awesome!');
}

runFunction(printMessage);

Tips When Writing Functions

  • Use meaningful names for your functions and parameters.
  • Don’t be afraid to split big functions into smaller ones.
  • Try to keep each function focused on doing just one thing.

Functions are your best friends when writing Dart code for Flutter. They help you write clean, organized, and efficient code. The more you use and experiment with functions, the more fluent you'll become in Dart and that’s a huge step toward mastering Flutter.


Understanding Data Types, Variables, and Operators in Flutter (Dart)

When you’re just getting started with Flutter, one thing you’ll quickly realize is that everything runs on Dart. Dart is the core language that powers Flutter, and like any other programming language, it has its own set of basic building blocks: data types, variables, and operators.

What Are Data Types?

Data types in Dart determine what kind of data a variable can hold. They help the compiler understand how much memory to allocate and how to handle the data.

  • int: Used for whole numbers. Example: int age = 25;
  • double: For decimal numbers. Example: double height = 5.9;
  • String: Text data. Example: String name = "Alice";
  • bool: Boolean values (true or false). Example: bool isLoggedIn = true;
  • List: Collection of items. Example: List<String> fruits = ['Apple', 'Banana'];

Declaring Variables

In Dart, variables can be declared using var, final, or const, depending on how you want them to behave.

  • var: Automatically detects the data type. var city = "New York";
  • final: Value can’t be changed once assigned. final country = "USA";
  • const: Like final, but value is known at compile-time. const pi = 3.14;

You can also be explicit:

String username = "flutter_dev";
int followers = 1000;
bool isVerified = false;

Understanding Operators

Operators are symbols used to perform operations on variables and values.

Arithmetic Operators

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • ~/ Integer Division
  • % Modulus (remainder)

Comparison Operators

  • == Equal to
  • != Not equal to
  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to

Logical Operators

  • && AND
  • || OR
  • ! NOT

Type Inference vs Explicit Typing

Dart supports type inference, meaning you don’t always need to specify the type of a variable. But you can if you want to improve readability or maintain strict type-checking.

var name = "John"; // Inferred as String
String surname = "Doe"; // Explicit

Mixing It All Together

Here’s a quick example that combines all these concepts:

void main() {
  int x = 10;
  int y = 5;
  int sum = x + y;
  print("Sum is $sum");

  bool isGreater = x > y;
  print("Is x greater than y? $isGreater");

  final message = "Flutter is fun!";
  print(message);
}

Understanding Dart’s basic elements like data types, variables, and operators will make your Flutter journey much smoother. These are the tools you'll use every day, so take some time to experiment and get comfortable with them!


Important Folders and Files in Flutter (main.dart, pubspec.yaml, etc.)

When diving into the world of Flutter development, one of the first things you'll encounter is the unique structure of its project. Unlike traditional app development that might feel a bit chaotic at first, Flutter gives us a relatively clean and organized layout. But, for beginners, that structure can still be a little confusing.

So, in this article, we’ll walk through the essential folders and files in a Flutter project. 

 

1. main.dart - The Entry Point

This is the heart of your Flutter app. Located in the lib/ folder, main.dart is the file where everything begins. It contains the main() function, which is the starting point of every Dart application.

void main() {
  runApp(MyApp());
}

Inside MyApp, you usually set up your app’s theme, routes, and home screen.

 

2. pubspec.yaml - The Configuration Brain

This file is where you manage your app’s metadata and dependencies. Want to use a new package like http or flutter_bloc? Just add it here. It also defines your app’s name, version, fonts, images, and more.

name: my_flutter_app
description: A new Flutter project
dependencies:
  flutter:
    sdk: flutter
  http: ^0.13.3

 

3. lib/ - The Code Playground

This is where all your Dart code lives. Typically, developers organize this folder into subfolders like:

  • screens/ – Each screen or page goes here
  • widgets/ – Reusable UI components
  • models/ – Data models
  • services/ – API services, database logic, etc.

You’re free to structure it the way you want, but it’s good practice to keep things modular and clean.

 

4. android/, ios/, and web/ - Platform Specific Code

Flutter is cross-platform, but sometimes you need to write native code or tweak settings. That’s what these folders are for.

  • android/ – Contains Gradle scripts and Android-specific settings
  • ios/ – Contains Xcode project files and iOS settings
  • web/ – For web-specific assets if you’re building for browser

 

5. test/ - Time to Test!

This folder is meant for unit and widget testing. If you write good tests here, you’ll catch bugs early and save yourself a ton of headaches later.

flutter test

 

6. Other Files and Folders

  • build/ – Auto-generated files after building your project
  • .dart_tool/ – Tool-related files (don’t edit)
  • .idea/ – Project config if you use Android Studio
  • .packages – List of all Dart packages used

You typically don’t need to touch these unless you know what you’re doing.


Getting familiar with Flutter’s project structure might take a day or two, but once you understand where everything lives, you’ll be way more confident in building apps.

As you continue your journey with Flutter, try exploring each file and folder on your own. Don’t be afraid to break stuff  that’s how you learn!


How to Install Flutter SDK on Windows, macOS, and Linux

Flutter is Google’s powerful open-source framework that lets you build cross-platform apps from a single codebase — for Android, iOS, web, and even desktop. If you want to be a productive developer with just one tech stack, Flutter is a solid choice.

This guide will walk you through the step-by-step installation of Flutter SDK on Windows, macOS, and Linux in a beginner-friendly way. Let’s get started!

 

General Requirements

  • 4 GB RAM minimum (more is better)
  • 2.8 GB disk space for Flutter SDK (not including Android Studio or emulator)
  • Stable internet connection
  • Git installed
  • Preferred code editor (VS Code, Android Studio, etc.)

 

1. Installing Flutter SDK on Windows

Step 1: Download the SDK

  • Go to flutter.dev and click "Get Started"
  • Select Windows
  • Download the ZIP file (e.g., flutter_windows_x.x.x-stable.zip)

Step 2: Extract and Set Up

Extract the ZIP to a clean path like C:\src\flutter (avoid spaces in the path).

Step 3: Add to PATH

  1. Search "Environment Variables" in Start Menu
  2. Edit the system environment variables → Environment Variables
  3. Edit the "Path" variable and add: C:\src\flutter\bin

Step 4: Verify Installation

flutter doctor

If you see the checklist, you’re good to go!

Step 5: Install Android Studio

  • Download and install Android Studio
  • Install Flutter and Dart plugins
  • Install Android SDK & emulator

 

2. Installing Flutter SDK on macOS

Step 1: Install Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 2: Install Git

brew install git

Step 3: Download Flutter

  • Visit flutter.dev → macOS → Download ZIP
  • Extract to ~/development/flutter

Step 4: Add to PATH

export PATH="$PATH:`pwd`/flutter/bin"

Then run:

source ~/.zshrc

Step 5: Run Flutter Doctor

flutter doctor

Step 6: Install Xcode (for iOS)

  • Install Xcode via App Store
  • Open Xcode → Preferences → Command Line Tools
  • Run:
    
    sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
    sudo xcodebuild -runFirstLaunch
        

Step 7: Install Android Studio

  • Same as Windows steps

 

3. Installing Flutter SDK on Linux

Step 1: Install Git & Dependencies


sudo apt update
sudo apt install git curl unzip xz-utils

Step 2: Download SDK


git clone https://github.com/flutter/flutter.git -b stable

Or download ZIP and extract to ~/development/flutter

Step 3: Add to PATH

export PATH="$PATH:$HOME/flutter/bin"
source ~/.bashrc

Step 4: Run Flutter Doctor

flutter doctor

Step 5: Install Android Studio

sudo snap install android-studio --classic

 

Tips

  • Use VS Code for lightweight projects
  • Check Flutter version: flutter --version
  • Update Flutter: flutter upgrade


Congrats! You now have Flutter SDK installed on your favorite OS. This is your first step toward building awesome cross-platform apps. From here, try creating your first Flutter app and explore the widgets, layouts, and state management tools.