About Me

I have decades of experience is software development using .Net Technologies, PHP and wordpress. I love coding and discovering new tech.

Blog

Flutter Basics: Conditions and Switch Statements (Quick Reference)

Coding, Flutter

Flutter Basics: Conditions and Switch Statements (Quick Reference)

Posted on July 18, 2025  - By Kaustav Halder - 0 Comments

In Dart (and most programming languages), conditional logic controls what happens when. Whether you’re checking values or switching between states, these tools help make decisions in your app. So we use them in checks and validations and make other cool stuff happen. Its like you are programming the brain of your applications using these.

if, else if, else

Use if to test a condition. Add else if and else for branching logic. So lets say i want to mark a users grade. If he scores above or equal to 90 he gets a grade of A, less than 90 but greater than or equal 75 its grade B else grade C. This is how you will code it in any language.

int score = 85;

if (score >= 90) {
  print("Grade: A");
} else if (score >= 75) {
  print("Grade: B");
} else {
  print("Grade: C or below");
}

Logical Operators

So you can combine multiple conditions by using logical operators and i want to check if a user is an admin and also the owner. I can do it as below.

if (age > 18 && hasID) {
  print("Access granted");
}

if (user == "admin" || user == "owner") {
  print("Welcome, privileged user");
}

Common Operators in Dart

OperatorMeaningExample
==Equal toa == b
!=Not equal tox != y
>Greater thanscore > 70
<Less thanage < 18
>=Greater than or equalvalue >= 100
<=Less than or equalitems <= 5
&&Logical ANDa > 0 && b < 10
!Logical NOT!isLoggedIn
%Modulo (remainder)7 % 3 // returns 1

Ternary Operator

Compact if-else. This is a short hand way of checking conditions. So you mention the condition then ? symbol followed by what happens if its true or false separated by a : symbol.

String status = (isOnline) ? "Online" : "Offline";

Nested ternary (use carefully). So you can actually go nuts with the Compact if else by nesting it but best to avoid this and only use it for small logic. Always remember that code is best if you can easily read. No point making it cool if its looking back at it after 1 year and you yourself can’t figure what you did.

String grade = (score >= 90)
  ? "A"
  : (score >= 75)
    ? "B"
    : "C";

switch Statement

Great for checking a value against multiple cases.

String role = "admin";

switch (role) {
  case "admin":
    print("You have full access");
    break;
  case "user":
    print("Limited access");
    break;
  default:
    print("Unknown role");
}

Summary

So just a summary on when to use what

StructureUse When…
ifYou want to test a condition
else ifMultiple branches of logic
elseFallback when no condition is met
switchChecking a single value against cases
Ternary ? :You want concise conditional assignment



About Kaustav

I have decades of experience is software development using .Net Technologies, PHP and wordpress. I love coding and discovering new tech.


0 Comments

Be the first to comment


Leave a reply

Leave a Reply

Your email address will not be published. Required fields are marked *