{"id":440,"date":"2026-11-08T18:01:15","date_gmt":"2026-11-08T18:01:15","guid":{"rendered":"https:\/\/moiks.com\/blog\/?p=440"},"modified":"2026-11-08T18:01:16","modified_gmt":"2026-11-08T18:01:16","slug":"dart-variables-data-types","status":"publish","type":"post","link":"https:\/\/moiks.com\/blog\/dart-variables-data-types\/","title":{"rendered":"Dart Programming Basics: Variables, Data Types, final, const &#038; var (2026 Guide)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Dart is the programming language behind Flutter, Google&#8217;s cross-platform UI framework for building applications for Android, iOS, web, and desktop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before learning advanced Flutter widgets, state management, networking, and application architecture, it is important to understand <strong>Dart variables and data types<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Variables allow applications to store and manage information, while data types determine what kinds of values those variables can hold.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dart also provides powerful language features such as type inference, sound null safety, compile-time constants, and immutable references that help developers write reliable and maintainable applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this beginner-friendly tutorial, we&#8217;ll explore Dart programming fundamentals through practical examples and build a small Flutter application that demonstrates how variables and data types work in real mobile development.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is <strong>Flutter Article 5<\/strong> in the Moiks Developer Blog Flutter Tutorials series.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What You&#8217;ll Learn<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this tutorial, you&#8217;ll understand:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What variables are in Dart.<\/li>\n\n\n\n<li>How to declare and initialize variables.<\/li>\n\n\n\n<li>The differences between <code>var<\/code>, <code>final<\/code>, and <code>const<\/code>.<\/li>\n\n\n\n<li>Dart&#8217;s built-in data types.<\/li>\n\n\n\n<li>How type inference works.<\/li>\n\n\n\n<li>How to use nullable and non-nullable variables.<\/li>\n\n\n\n<li>When to use <code>late<\/code> variables.<\/li>\n\n\n\n<li>The differences between <code>Object<\/code>, <code>Object?<\/code>, and <code>dynamic<\/code>.<\/li>\n\n\n\n<li>How collections store multiple values.<\/li>\n\n\n\n<li>How Dart records and patterns work.<\/li>\n\n\n\n<li>How to use variables inside Flutter widgets.<\/li>\n\n\n\n<li>Common mistakes, testing techniques, and best practices.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. What Is Dart Programming Language?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dart is a modern, object-oriented, strongly typed programming language developed by Google.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is the primary programming language used to build Flutter applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dart supports features such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Static typing with type inference.<\/li>\n\n\n\n<li>Sound null safety.<\/li>\n\n\n\n<li>Object-oriented programming.<\/li>\n\n\n\n<li>Asynchronous programming using Future and Stream.<\/li>\n\n\n\n<li>Generics and collections.<\/li>\n\n\n\n<li>Pattern matching and records.<\/li>\n\n\n\n<li>Compile-time constants.<\/li>\n\n\n\n<li>Just-in-time compilation for supported development workflows.<\/li>\n\n\n\n<li>Ahead-of-time compilation for supported production targets.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">One of Dart&#8217;s advantages is its integration with Flutter&#8217;s development workflow, including hot reload.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Your First Dart Program<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  print('Welcome to Dart Programming!');\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to Dart Programming!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>main()<\/code> function is the entry point of a standalone Dart program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>print()<\/code> function displays a value in the console.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can execute this example using DartPad:<\/p>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/dartpad.dev\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Alternatively, create a file named <code>main.dart<\/code> and run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dart run main.dart<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. What Are Variables in Dart?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A variable is a named reference used to access a value in a program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, a Flutter application might store a user&#8217;s name, age, account balance, or selected theme.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  String userName = 'Krishna';\n  int age = 28;\n  double accountBalance = 1250.75;\n  bool isLoggedIn = true;\n\n  print(userName);\n  print(age);\n  print(accountBalance);\n  print(isLoggedIn);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Krishna\n28\n1250.75\ntrue<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each variable has a type that determines which values can be assigned to it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, an <code>int<\/code> variable stores integer values, while a <code>String<\/code> variable stores text.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dart is statically typed, which means its type system checks assignments and operations for compatibility.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. How to Declare Variables in Dart<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">There are several ways to declare variables.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Explicit Type Declaration<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>String appName = 'Moiks Flutter';\nint downloadCount = 150;\ndouble rating = 4.8;\nbool isPremium = false;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the variable types are explicitly declared.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using var<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>var appName = 'Moiks Flutter';\nvar downloadCount = 150;\nvar rating = 4.8;\nvar isPremium = false;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Dart automatically infers the type from the assigned value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var appName = 'Moiks Flutter';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Dart infers that <code>appName<\/code> is a <code>String<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The variable cannot later hold an incompatible type.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var appName = 'Flutter';\n\n\/\/ Compilation error:\nappName = 100;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Important:<\/strong> <code>var<\/code> does not mean the variable is dynamically typed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once Dart infers its static type, normal type checking applies.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Dart Built-in Data Types<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dart provides several built-in types for storing different kinds of information.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Data type<\/th><th>Purpose<\/th><th>Example<\/th><\/tr><tr><td><code>int<\/code><\/td><td>Integer numbers<\/td><td><code>42<\/code><\/td><\/tr><tr><td><code>double<\/code><\/td><td>Decimal numbers<\/td><td><code>4.75<\/code><\/td><\/tr><tr><td><code>num<\/code><\/td><td>Integer or double<\/td><td><code>10<\/code>, <code>3.5<\/code><\/td><\/tr><tr><td><code>String<\/code><\/td><td>Text<\/td><td><code>'Flutter'<\/code><\/td><\/tr><tr><td><code>bool<\/code><\/td><td>Boolean values<\/td><td><code>true<\/code><\/td><\/tr><tr><td><code>List<\/code><\/td><td>Ordered collection<\/td><td><code>[1, 2, 3]<\/code><\/td><\/tr><tr><td><code>Set<\/code><\/td><td>Unique elements<\/td><td><code>{1, 2, 3}<\/code><\/td><\/tr><tr><td><code>Map<\/code><\/td><td>Key-value pairs<\/td><td><code>{'name': 'Dart'}<\/code><\/td><\/tr><tr><td><code>Record<\/code><\/td><td>Fixed-shape grouped values<\/td><td><code>('Dart', 3)<\/code><\/td><\/tr><tr><td><code>Null<\/code><\/td><td>Null value type<\/td><td><code>null<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s understand the most frequently used types.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Integer Data Type \u2014 int<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>int<\/code> type represents integer values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  int userAge = 25;\n  int totalDownloads = 10000;\n  int remainingLives = 3;\n\n  print(userAge);\n  print(totalDownloads);\n  print(remainingLives);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Integers are commonly used for counters, indexes, quantities, and identifiers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Arithmetic Operations<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  int a = 20;\n  int b = 6;\n\n  print(a + b);  \/\/ 26\n  print(a - b);  \/\/ 14\n  print(a * b);  \/\/ 120\n  print(a \/ b);  \/\/ 3.333...\n  print(a ~\/ b); \/\/ 3\n  print(a % b);  \/\/ 2\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>\/<\/code> operator performs division and returns a <code>double<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>~\/<\/code> operator performs truncating division and returns an integer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>%<\/code> operator calculates the remainder.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Decimal Data Type \u2014 double<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>double<\/code> type represents double-precision floating-point numbers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  double productPrice = 499.99;\n  double appRating = 4.7;\n  double screenWidth = 393.0;\n\n  print(productPrice);\n  print(appRating);\n  print(screenWidth);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Doubles are useful for measurements, UI dimensions, animation values, and approximate numeric calculations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important: Floating-Point Precision<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Binary floating-point arithmetic can produce small rounding differences.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  print(0.1 + 0.2);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The result may be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0.30000000000000004<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For financial calculations requiring exact decimal amounts, consider representing money in integer minor units, such as paise or cents, or using a suitable decimal arithmetic library.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do not assume <code>double<\/code> is appropriate for every financial calculation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Using the num Data Type<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>num<\/code> type is a common numeric supertype of <code>int<\/code> and <code>double<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  num value = 100;\n\n  print(value);\n\n  value = 99.5;\n\n  print(value);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>100\n99.5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>num<\/code> when an API genuinely needs to accept both integer and floating-point values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer <code>int<\/code> or <code>double<\/code> when the expected numeric representation is known.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. String Data Type<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>String<\/code> represents text.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  String firstName = 'Moiks';\n  String lastName = \"Studio\";\n\n  print(firstName);\n  print(lastName);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Dart supports single and double quotation marks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">String Interpolation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">String interpolation allows variables and expressions to be inserted into text.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  String appName = 'Flutter';\n  int version = 3;\n\n  print('Welcome to $appName');\n  print('Major version: $version');\n  print('Next major version: ${version + 1}');\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to Flutter\nMajor version: 3\nNext major version: 4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>$variable<\/code> for simple variable references and <code>${expression}<\/code> for more complex expressions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Multiline Strings<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  String description = '''\nFlutter supports\nAndroid and iOS\ndevelopment using Dart.\n''';\n\n  print(description);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Useful String Operations<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  String title = 'Flutter Development';\n\n  print(title.length);\n  print(title.toUpperCase());\n  print(title.toLowerCase());\n  print(title.contains('Flutter'));\n  print(title.replaceAll('Flutter', 'Dart'));\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">String operations are frequently used in search, forms, validation, and UI formatting.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9. Boolean Data Type \u2014 bool<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>bool<\/code> type represents <code>true<\/code> or <code>false<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  bool isLoggedIn = true;\n  bool isPremiumUser = false;\n\n  print(isLoggedIn);\n  print(isPremiumUser);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Booleans are useful for conditional application behavior.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  bool isPremium = true;\n\n  if (isPremium) {\n    print('Premium features enabled');\n  } else {\n    print('Free version enabled');\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike some languages, Dart does not implicitly convert integers or strings into Boolean values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conditions must evaluate to <code>bool<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">10. Understanding var in Dart<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>var<\/code> keyword lets Dart infer a variable&#8217;s type.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  var name = 'Flutter';\n  var age = 10;\n  var rating = 4.9;\n  var isActive = true;\n\n  print(name.runtimeType);\n  print(age.runtimeType);\n  print(rating.runtimeType);\n  print(isActive.runtimeType);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String\nint\ndouble\nbool<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">When Should You Use var?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>var<\/code> when the type is obvious from the initializer.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var userName = 'Alex';\nvar totalItems = 20;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An explicit type can be useful when it communicates important intent.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>num amount = 100;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Dart&#8217;s effective style guidance generally favors inferred types for local variables when the type is already clear.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">11. Understanding final in Dart<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>final<\/code> keyword declares a variable that can be assigned only once.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  final userId = 'USER_1001';\n\n  print(userId);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Attempting to reassign the variable causes a compile-time error.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>final userId = 'USER_1001';\n\n\/\/ Error:\nuserId = 'USER_1002';<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">final with Runtime Values<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>final<\/code> variable can be initialized using a value calculated at runtime.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  final currentTime = DateTime.now();\n\n  print(currentTime);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This works because <code>final<\/code> does not require compile-time constant initialization.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">final Does Not Automatically Make Objects Immutable<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Consider:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  final numbers = &lt;int&gt;&#91;1, 2, 3];\n\n  numbers.add(4);\n\n  print(numbers);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 2, 3, 4]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The variable cannot be reassigned, but the referenced list remains mutable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This distinction is especially important when working with Flutter state.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">12. Understanding const in Dart<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>const<\/code> keyword creates a compile-time constant variable or constant value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  const appName = 'Moiks Flutter';\n  const maxAttempts = 3;\n  const piValue = 3.14159;\n\n  print(appName);\n  print(maxAttempts);\n  print(piValue);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>const<\/code> variable must be initialized using a compile-time constant expression.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">const with Collections<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  const supportedPlatforms = &#91;\n    'Android',\n    'iOS',\n  ];\n\n  print(supportedPlatforms);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You cannot modify this constant list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const platforms = &#91;'Android', 'iOS'];\n\n\/\/ Runtime error if executed:\nplatforms.add('Web');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The attempted mutation throws an <code>UnsupportedError<\/code> because the constant collection is unmodifiable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">const with Runtime Values<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is invalid:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Compilation error:\nconst currentTime = DateTime.now();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>DateTime.now()<\/code> is evaluated at runtime.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>final<\/code> instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">13. Dart final vs const vs var<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding these keywords is essential for writing clean Dart and Flutter code.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Feature<\/th><th>var<\/th><th>final<\/th><th>const<\/th><\/tr><tr><td>Type inference<\/td><td>Yes<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Reassignment<\/td><td>Allowed<\/td><td>Not allowed<\/td><td>Not allowed<\/td><\/tr><tr><td>Runtime initialization<\/td><td>Yes<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Compile-time constant required<\/td><td>No<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Mutable referenced objects possible<\/td><td>Yes<\/td><td>Yes<\/td><td>Constant values are immutable<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  var counter = 0;\n  final startedAt = DateTime.now();\n  const maximumCount = 10;\n\n  counter++;\n\n  print(counter);\n  print(startedAt);\n  print(maximumCount);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>const<\/code> for compile-time constants.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>final<\/code> for values that should be assigned only once.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>var<\/code> for local variables that need reassignment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Choose based on intended behavior, not merely personal preference.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">14. Understanding late Variables<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dart requires non-nullable variables to be initialized before they are used.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>late<\/code> modifier allows initialization to happen later.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>late String userName;\n\nvoid main() {\n  userName = 'Flutter Developer';\n\n  print(userName);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Lazy Initialization<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>late<\/code> variable with an initializer is initialized when first accessed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String loadConfiguration() {\n  print('Loading configuration...');\n  return 'Production';\n}\n\nvoid main() {\n  late final configuration = loadConfiguration();\n\n  print('Application started');\n\n  print(configuration);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Application started\nLoading configuration...\nProduction<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important Warning<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Accessing an uninitialized <code>late<\/code> variable causes a runtime error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>late<\/code> only when delayed initialization is intentional and safe.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid using it simply to silence initialization errors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">15. Understanding Dart Null Safety<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dart uses sound null safety.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By default, a variable with a non-nullable type cannot contain <code>null<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String userName = 'Flutter';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This variable cannot be assigned <code>null<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Nullable Variables<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add <code>?<\/code> to allow a variable to contain <code>null<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String? userName;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now the variable can contain either a <code>String<\/code> or <code>null<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  String? userName;\n\n  print(userName);\n\n  userName = 'Moiks';\n\n  print(userName);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Null-Aware Access<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>?.<\/code> to access a property only when the value is non-null.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  String? name;\n\n  print(name?.length);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>null<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Null-Coalescing Operator<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>??<\/code> operator provides a fallback value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  String? displayName;\n\n  String result = displayName ?? 'Guest';\n\n  print(result);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Guest<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Null Assertion Operator<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>!<\/code> operator tells Dart to treat a nullable expression as non-null.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String? name = 'Flutter';\n\nprint(name!.length);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>!<\/code> carefully.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the value is actually <code>null<\/code>, the operation throws at runtime.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer explicit null checks, null-aware operators, or appropriate default values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">16. Object, Object? and dynamic<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dart provides different ways to represent values when the exact type is not known.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Object<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An <code>Object<\/code> variable can reference non-null objects of different types.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  Object value = 'Flutter';\n\n  value = 100;\n  value = true;\n\n  print(value);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">However, Dart still enforces static type checking when accessing members.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Object?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Object?<\/code> allows any object reference, including <code>null<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Object? value = null;\n\nvalue = 'Dart';\nvalue = 25;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">dynamic<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>dynamic<\/code> allows operations to bypass many static type checks.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  dynamic value = 'Flutter';\n\n  value = 100;\n\n  print(value);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Be careful when invoking members on dynamic values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dynamic value = 100;\n\n\/\/ May throw at runtime:\nvalue.toUpperCase();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer specific types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>Object?<\/code> when a value may have different types and requires runtime validation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>dynamic<\/code> only when its flexibility is genuinely necessary, such as at certain dynamic data boundaries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">17. Dart Lists<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>List<\/code> stores an ordered collection of values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  List&lt;String&gt; platforms = &#91;\n    'Android',\n    'iOS',\n    'Web',\n  ];\n\n  print(platforms);\n  print(platforms.first);\n  print(platforms.length);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Modifying a List<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  final languages = &lt;String&gt;&#91;\n    'Dart',\n    'Kotlin',\n  ];\n\n  languages.add('Swift');\n\n  print(languages);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Dart, Kotlin, Swift]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The list remains mutable even though the variable is declared <code>final<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Immutable List<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const languages = &lt;String&gt;&#91;\n  'Dart',\n  'Kotlin',\n  'Swift',\n];<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A constant list cannot be modified.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">18. Dart Sets<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>Set<\/code> stores unique elements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  final numbers = &lt;int&gt;{\n    1,\n    2,\n    3,\n  };\n\n  numbers.add(2);\n\n  print(numbers);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The set still contains only one occurrence of <code>2<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sets are useful for tracking unique identifiers, selected filters, and distinct values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">19. Dart Maps<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>Map<\/code> stores key-value pairs.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  final user = &lt;String, Object&gt;{\n    'name': 'Alex',\n    'age': 25,\n    'premium': true,\n  };\n\n  print(user&#91;'name']);\n  print(user&#91;'age']);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Maps are frequently used when working with JSON responses and application configuration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Typed Maps<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For more predictable values, use explicit key and value types.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>final scores = &lt;String, int&gt;{\n  'Level 1': 100,\n  'Level 2': 250,\n};\n\nprint(scores&#91;'Level 1']);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When handling real API responses, validate incoming data before assuming that it contains the expected types.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">20. Dart Records and Pattern Destructuring<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Modern Dart supports records, which group multiple typed values without defining a separate class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  final user = (\n    name: 'Alex',\n    age: 25,\n    premium: true,\n  );\n\n  print(user.name);\n  print(user.age);\n  print(user.premium);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Records are immutable and have a fixed shape.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Returning Multiple Values<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>(String, int) getUserInfo() {\n  return ('Flutter Developer', 5);\n}\n\nvoid main() {\n  final (name, experience) = getUserInfo();\n\n  print(name);\n  print(experience);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This uses record destructuring with a variable pattern.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Records and patterns are useful for small structured results and modern Dart control flow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For larger business entities, dedicated classes or models may be more appropriate.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">21. Practical Flutter Example: Dart Variables Dashboard<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s build a small Flutter application that demonstrates variables, <code>final<\/code>, <code>const<\/code>, nullable values, and state updates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The application will display a user&#8217;s profile and allow the user to increase a points counter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create a Flutter Project<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>flutter create dart_variables_demo\ncd dart_variables_demo<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Open <code>lib\/main.dart<\/code> and replace the generated code with the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'package:flutter\/material.dart';\n\nvoid main() {\n  runApp(const VariablesDemoApp());\n}\n\nclass VariablesDemoApp extends StatelessWidget {\n  const VariablesDemoApp({super.key});\n\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      title: 'Dart Variables Demo',\n      debugShowCheckedModeBanner: false,\n      theme: ThemeData(\n        colorScheme: ColorScheme.fromSeed(\n          seedColor: const Color(0xFF004B64),\n        ),\n      ),\n      home: const VariablesDashboard(),\n    );\n  }\n}\n\nclass VariablesDashboard extends StatefulWidget {\n  const VariablesDashboard({super.key});\n\n  @override\n  State&lt;VariablesDashboard&gt; createState() =&gt;\n      _VariablesDashboardState();\n}\n\nclass _VariablesDashboardState\n    extends State&lt;VariablesDashboard&gt; {\n  static const int maximumPoints = 100;\n\n  final String userName = 'Flutter Developer';\n  final DateTime sessionStarted = DateTime.now();\n\n  String? userBio;\n  int points = 0;\n  bool isPremium = false;\n\n  void increasePoints() {\n    setState(() {\n      if (points &lt; maximumPoints) {\n        points += 10;\n      }\n    });\n  }\n\n  void togglePremium() {\n    setState(() {\n      isPremium = !isPremium;\n    });\n  }\n\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      appBar: AppBar(\n        title: const Text('Dart Variables'),\n      ),\n      body: Center(\n        child: SingleChildScrollView(\n          padding: const EdgeInsets.all(24),\n          child: Column(\n            mainAxisSize: MainAxisSize.min,\n            children: &#91;\n              const Icon(\n                Icons.account_circle,\n                size: 90,\n                color: Color(0xFF004B64),\n              ),\n              const SizedBox(height: 16),\n              Text(\n                userName,\n                style: Theme.of(context)\n                    .textTheme\n                    .headlineSmall,\n              ),\n              const SizedBox(height: 8),\n              Text(userBio ?? 'No bio available'),\n              const SizedBox(height: 16),\n              Text(\n                'Points: $points \/ $maximumPoints',\n              ),\n              const SizedBox(height: 8),\n              Text(\n                'Account: '\n                '${isPremium ? \"Premium\" : \"Free\"}',\n              ),\n              const SizedBox(height: 8),\n              Text(\n                'Session: '\n                '${sessionStarted.hour}:'\n                '${sessionStarted.minute.toString().padLeft(2, \"0\")}',\n              ),\n              const SizedBox(height: 24),\n              FilledButton.icon(\n                onPressed: points &lt; maximumPoints\n                    ? increasePoints\n                    : null,\n                icon: const Icon(Icons.add),\n                label: const Text('Add 10 Points'),\n              ),\n              const SizedBox(height: 12),\n              OutlinedButton(\n                onPressed: togglePremium,\n                child: const Text('Toggle Premium'),\n              ),\n            ],\n          ),\n        ),\n      ),\n    );\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Understanding the Application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This example demonstrates several Dart concepts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>const:<\/strong> <code>maximumPoints<\/code> is a compile-time constant.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>final:<\/strong> <code>userName<\/code> and <code>sessionStarted<\/code> are assigned once.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Nullable String:<\/strong> <code>userBio<\/code> can contain a string or <code>null<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>int:<\/strong> <code>points<\/code> stores a whole-number score.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>bool:<\/strong> <code>isPremium<\/code> controls the account label.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>setState():<\/strong> Notifies Flutter that local state has changed and the widget needs rebuilding.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Expected Result<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The application displays:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>User profile icon.<\/li>\n\n\n\n<li>Flutter Developer name.<\/li>\n\n\n\n<li>Optional biography.<\/li>\n\n\n\n<li>Points counter.<\/li>\n\n\n\n<li>Premium account status.<\/li>\n\n\n\n<li>Session start time.<\/li>\n\n\n\n<li>Add 10 Points button.<\/li>\n\n\n\n<li>Toggle Premium button.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Each tap on Add 10 Points increases the counter until it reaches 100.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The points button is disabled once the maximum is reached.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tapping Toggle Premium switches the displayed account type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same application code can run on Android and iOS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">22. Why final and const Matter in Flutter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Flutter applications are built using widgets.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Widgets are immutable configuration objects, and many widgets support <code>const<\/code> constructors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const Text('Welcome to Flutter');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When all constructor arguments are compile-time constants, using <code>const<\/code> can allow Flutter to reuse canonicalized widget instances and avoid unnecessary allocations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, <code>const<\/code> does not automatically prevent every widget rebuild or guarantee better performance in every situation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Column(\n  children: &#91;\n    const Text('Account Details'),\n    const SizedBox(height: 16),\n    Text('Points: $points'),\n  ],\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The heading and spacing are constant.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The points label is dynamic because its text changes with application state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>const<\/code> when the constructor and its arguments support it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid forcing <code>const<\/code> onto values that depend on runtime state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For performance-sensitive applications, profile real behavior using Flutter DevTools.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">23. Testing Dart Variables and Flutter State<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dart supports automated unit tests, while Flutter provides widget testing tools.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s test the variables dashboard.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create or replace:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>test\/widget_test.dart<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'package:flutter\/material.dart';\nimport 'package:flutter_test\/flutter_test.dart';\nimport 'package:dart_variables_demo\/main.dart';\n\nvoid main() {\n  testWidgets(\n    'Points increase when button is tapped',\n    (WidgetTester tester) async {\n      await tester.pumpWidget(\n        const VariablesDemoApp(),\n      );\n\n      expect(\n        find.text('Points: 0 \/ 100'),\n        findsOneWidget,\n      );\n\n      await tester.tap(\n        find.widgetWithText(\n          FilledButton,\n          'Add 10 Points',\n        ),\n      );\n\n      await tester.pump();\n\n      expect(\n        find.text('Points: 10 \/ 100'),\n        findsOneWidget,\n      );\n    },\n  );\n\n  testWidgets(\n    'Premium status toggles correctly',\n    (WidgetTester tester) async {\n      await tester.pumpWidget(\n        const VariablesDemoApp(),\n      );\n\n      expect(\n        find.text('Account: Free'),\n        findsOneWidget,\n      );\n\n      await tester.tap(\n        find.text('Toggle Premium'),\n      );\n\n      await tester.pump();\n\n      expect(\n        find.text('Account: Premium'),\n        findsOneWidget,\n      );\n    },\n  );\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>flutter test<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Also run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>flutter analyze<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you use a different project name, update the package import in the test file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Additional Testing Recommendations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For production applications:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Test nullable values.<\/li>\n\n\n\n<li>Test minimum and maximum numeric boundaries.<\/li>\n\n\n\n<li>Test invalid user input.<\/li>\n\n\n\n<li>Test state transitions.<\/li>\n\n\n\n<li>Validate external data before assigning it to models.<\/li>\n\n\n\n<li>Test important workflows on both Android and iOS.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">24. Common Dart Variable Errors and Troubleshooting<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Error 1: A Value Can&#8217;t Be Assigned to a Variable of Another Type<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int age = 25;\n\n\/\/ Error:\nage = 'Twenty Five';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Solution:<\/strong> Assign a compatible value or explicitly convert data when appropriate.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>final age = int.parse('25');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When parsing untrusted input, prefer <code>int.tryParse()<\/code> and handle invalid values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Error 2: Cannot Assign to a final Variable<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>final userName = 'Alex';\n\n\/\/ Error:\nuserName = 'Sam';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Solution:<\/strong> Use a non-final variable if reassignment is intentional.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Error 3: const Requires a Constant Value<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Error:\nconst currentTime = DateTime.now();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Solution:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>final currentTime = DateTime.now();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Error 4: Non-Nullable Variable Is Not Initialized<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A non-nullable instance field must be initialized before use.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class User {\n  String name;\n\n  User(this.name);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use constructor initialization, an initializer, or another valid initialization strategy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Error 5: LateInitializationError<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>late String token;\n\nvoid main() {\n  print(token);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This throws at runtime because <code>token<\/code> has not been initialized.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Solution:<\/strong> Initialize the variable before accessing it, or redesign the code to avoid unsafe delayed initialization.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Error 6: Null Check Operator Used on a Null Value<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>String? name;\n\nprint(name!.length);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This throws at runtime.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Solution:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(name?.length ?? 0);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Error 7: Unsupported Operation on a const List<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const numbers = &#91;1, 2, 3];\n\nnumbers.add(4);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This throws an <code>UnsupportedError<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Solution:<\/strong> Use a mutable list when modifications are required.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">25. Dart Variable Best Practices<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Prefer Strong Typing<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use specific types when possible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid unnecessary use of <code>dynamic<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Use final by Default for Stable References<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When a variable does not need reassignment, <code>final<\/code> communicates that intention clearly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Use const for Compile-Time Constants<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>const<\/code> for fixed configuration values and eligible Flutter widgets.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Handle Nullable Values Explicitly<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>?<\/code>, <code>?.<\/code>, and <code>??<\/code> appropriately.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid excessive null assertions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Avoid Unnecessary late Variables<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer constructor initialization or direct initialization whenever possible.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Use Meaningful Variable Names<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Good:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>final userEmail = 'user@example.com';\nfinal totalDownloads = 1500;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Less descriptive:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>final x = 'user@example.com';\nfinal y = 1500;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Keep Business Logic Separate from Widgets<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For larger Flutter applications, move complex business calculations into dedicated classes or state management components.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Avoid Premature Optimization<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Correct typing, clear state ownership, and predictable data flow matter more than micro-optimizing simple variable declarations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">26. Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is a variable in Dart?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A variable is a named reference that allows a program to access a value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between var and dynamic in Dart?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>var<\/code> uses static type inference when an initializer provides a type. <code>dynamic<\/code> relaxes static checking of operations and may defer errors until runtime.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between final and const?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>final<\/code> allows assignment once, including runtime initialization. <code>const<\/code> requires a compile-time constant value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should I use var or final in Dart?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>final<\/code> when reassignment is unnecessary. Use <code>var<\/code> when a local variable needs to change.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can a final List be modified?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, if the referenced list is mutable. <code>final<\/code> prevents reassignment of the variable, not mutation of the object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is Dart a strongly typed language?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Dart has a sound static type system, with type inference and support for dynamic operations when explicitly requested.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is null safety in Dart?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Null safety helps prevent accidental access to null values by making types non-nullable by default and requiring explicit nullable declarations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is late in Dart?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>late<\/code> supports delayed initialization and lazy initialization. Accessing an uninitialized late variable can cause a runtime error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What are Dart records?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Records are immutable, fixed-shape values that can group multiple typed fields without defining a dedicated class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does Flutter use the same Dart data types on Android and iOS?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Shared Dart code uses the same language types on both platforms, although native interoperability and platform APIs may require additional conversions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">27. Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding Dart variables and data types is essential for building reliable Flutter applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we explored Dart&#8217;s built-in types, variable declarations, type inference, <code>var<\/code>, <code>final<\/code>, <code>const<\/code>, <code>late<\/code>, null safety, collections, and records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We also built a practical Flutter variables dashboard and added widget tests to verify state updates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The key lesson is simple: <strong>choose types and variable modifiers that clearly express how your data should behave.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These fundamentals will help you write safer Dart code, manage Flutter widget state, and prepare for more advanced programming concepts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What&#8217;s Next in the Flutter Tutorials Series?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Flutter Article 6: Dart Operators Explained \u2014 Arithmetic, Comparison, Logical, Null-Aware &amp; Cascade Operators<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the next tutorial, we&#8217;ll explore Dart operators with practical examples, including calculations, comparisons, Boolean expressions, null-aware operations, and cascade notation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Continue learning Flutter and Dart at:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Moiks Developer Blog:<\/strong> <a href=\"https:\/\/moiks.com\/blog\/\">https:\/\/moiks.com\/blog\/<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Documentation and References<\/h2>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Dart Language Overview \u2014 <a href=\"https:\/\/dart.dev\/language\" target=\"_blank\" rel=\"noopener\">https:\/\/dart.dev\/language<\/a><\/li>\n\n\n\n<li>Dart Variables \u2014 <a href=\"https:\/\/dart.dev\/language\/variables\" target=\"_blank\" rel=\"noopener\">https:\/\/dart.dev\/language\/variables<\/a><\/li>\n\n\n\n<li>Dart Built-in Types \u2014 <a href=\"https:\/\/dart.dev\/language\/built-in-types\" target=\"_blank\" rel=\"noopener\">https:\/\/dart.dev\/language\/built-in-types<\/a><\/li>\n\n\n\n<li>Dart Null Safety \u2014 <a href=\"https:\/\/dart.dev\/null-safety\" target=\"_blank\" rel=\"noopener\">https:\/\/dart.dev\/null-safety<\/a><\/li>\n\n\n\n<li>Dart Collections \u2014 <a href=\"https:\/\/dart.dev\/language\/collections\" target=\"_blank\" rel=\"noopener\">https:\/\/dart.dev\/language\/collections<\/a><\/li>\n\n\n\n<li>Dart Records \u2014 <a href=\"https:\/\/dart.dev\/language\/records\" target=\"_blank\" rel=\"noopener\">https:\/\/dart.dev\/language\/records<\/a><\/li>\n\n\n\n<li>Dart Patterns \u2014 <a href=\"https:\/\/dart.dev\/language\/patterns\" target=\"_blank\" rel=\"noopener\">https:\/\/dart.dev\/language\/patterns<\/a><\/li>\n\n\n\n<li>Dart Effective Style \u2014 <a href=\"https:\/\/dart.dev\/effective-dart\/style\" target=\"_blank\" rel=\"noopener\">https:\/\/dart.dev\/effective-dart\/style<\/a><\/li>\n\n\n\n<li>Flutter State Management Fundamentals \u2014 <a href=\"https:\/\/docs.flutter.dev\/data-and-backend\/state-mgmt\/ephemeral-vs-app\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.flutter.dev\/data-and-backend\/state-mgmt\/ephemeral-vs-app<\/a><\/li>\n\n\n\n<li>Flutter Testing Overview \u2014 <a href=\"https:\/\/docs.flutter.dev\/testing\/overview\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.flutter.dev\/testing\/overview<\/a><\/li>\n\n\n\n<li>DartPad \u2014 <a href=\"https:\/\/dartpad.dev\/\" target=\"_blank\" rel=\"noopener\">https:\/\/dartpad.dev\/<\/a><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Moiks Developer Blog \u2014 Practical Flutter, Dart, Android, and iOS development tutorials.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Master Dart programming fundamentals with beginner-friendly examples. Learn variables, data types, var, final, const, late, type inference, null safety, collections, and how these concepts work in real Flutter applications.<\/p>\n","protected":false},"author":1,"featured_media":441,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0,"footnotes":""},"categories":[353,397],"tags":[347,389,391,390,392,394,396,393,395,351,368],"class_list":["post-440","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flutter-tutorials","category-dart-programming","tag-dart","tag-dart-data-types","tag-dart-final-vs-const","tag-dart-late","tag-dart-null-safety","tag-dart-programming","tag-dart-type-inference","tag-dart-var","tag-dart-variables","tag-flutter","tag-flutter-beginners"],"_links":{"self":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/comments?post=440"}],"version-history":[{"count":1,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/440\/revisions"}],"predecessor-version":[{"id":442,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/440\/revisions\/442"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media\/441"}],"wp:attachment":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media?parent=440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/categories?post=440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/tags?post=440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}