Variables In Dart

What is Variables?

1. Variables are containers used to store value in the program.
2. Variables can be of different types, such as integers, floating-point numbers, strings, Booleans, and more.
3. To declare a variable in Dart, you use the var keyword followed by the variable name and an optional value
4. Syntax – var variableName = value;
5. Syntax – type variableName = value; //here type is datatype

var name = 'Nandan';
var age = 23;
var isMarried = false;

You can also specify the type of a variable explicitly using the Type syntax:

String name = 'Nandan';
int age = 23;
bool isMarried = false;

Some Rules In Variables

1. Variable names are case sensitive, i.e., a and A are different.
2. A variable name can consist of letters and alphabets.
3. A variable name cannot start with number.
4. Keywords are not allowed to use as a variable name.
5. Blank spaces are not allowed in a variable name.
6. Special characters are not allowed except for the underscore (_) and the dollar ($) sign.

Types Of Variables

1. String: For storing text value. E.g. “Nandan” [Must be in quotes]
2. int: For storing integer value. E.g. 7, -10, 1234 [Decimal is not included]
3. double: For storing floating point value. E.g. 10.0, -10.2, 85.698 [Decimal is included]
4. num: For storing any types of number. E.g. 10, 20.2, -20 [both int and double]
5. bool: For storing true or false value. E.g. true, false [Only stores true or false values]
6. var: For storing any value. E.g. ‘Small’, 12, ‘z’, true, false

Using constant in dart

Constant is the type of variable whose value never changes.

void main() {
  String name = "Nandan";
  String address = "Karnataka";
  int age = 23;
  bool isDegreeFinished = true;
  var info = "Curious Developers Community";

  print("Name is $name");
  print("Address is $address");
  print("Age is $age");
  print("Degree Status is $isDegreeFinished");
  print("info is $info");

  //constant in dart

  const String constant_info = "Nochange";
  print("No Change in information $constant_info");
}

/*
Name is Nandan
Address is Karnataka
Age is 23
Degree Status is true
info is Curious Developers Community

No Change in information Nochange
*/

Naming Conventions

In Dart the variable name should start with lower-case, and every second word’s first letter will be upper-case like num1, fullName, isMarried, etc. Technically, this naming convention is called lowerCamelCase.

// Incorrect Way
var fullname = "Nandan";

// Correct Way
var fullName = "Nandan";

const pi = 3.14;

Project – 2 Details Generator

In this project we will generate a simple details of the person by using variables

void main()
{
    String name = "Nandan";
    String email = "info@curiousdevelopers.in";
    int rollNum = 7;
    int age = 23;
    
    print("My name is $name, I'm $age years old, My roll number is $rollNum and My email id is $email");
    
}

/*
My name is Nandan, I'm 23 years old, My roll number is 7 and My email id is info@curiousdevelopers.in
*/

Leave a Comment

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

Scroll to Top