• Guides
    • Getting started with Dart
  • Blog
  • FAQ
    • Dart
    • Flutter
  • Contact
  • Guides
    • Getting started with Dart
  • Blog
  • FAQ
    • Dart
    • Flutter
  • Contact
  • Getting Started with Dart
    • Dart Variables
    • Dart Operators
    • Dart Comments
  • Dart Data Types
    • Dart Strings
    • Dart Numbers
    • Dart Booleans
    • Dart Lists
    • Dart Maps
  • Control Flow
    • Dart if...else Statements
    • Dart switch Statement
    • Dart for Loop
    • Dart for...in Loop
    • Dart while and do...while Loops
    • break and continue Statements
  • Dart Functions
  • Dart Classes
  • Dart Objects
  • Dart Constructors
  • Dart Exceptions

Dart Variables

388 views 1

Written by fawesomeapps
December 7, 2021

In this chapter, we will learn about Dart variables and constants in dart, how to declare and initialize them.


Dart Variables

Variable is an identifier that stores data. For example,

var name = “Jackson”;

Here name is a variable that stores the string data Jackson.


Declaring Variables

To declare variables, keyword var is used. For example,

var number;

Dart also supports type-checking which helps in declaring the variables. For example,

int number; // for integer values

String name; // for string values

Initialising Variables

To assign a value to variables, the assignment operator (=) is used. For example,

var number = 5;

Here number is a variable and value 5 is assigned to the number variable.

Similarly we can pass specific values to the variables. For example,

String name = “Lauren”;

If we try to pass integer values to string variable, then we get the error.

String name = 4; // Error: A value of type 'int' can't be assigned to a variable of type 'String'.

Changing values of variables

The variables mean that the values can be changed. We can simply change the value of a variable by assigning a new value to the variable.

For example,

var name = ‘Sam’;

name = ‘John’;

Rules for naming a Dart variable

  1. Variable names can consist of numbers and alphabets, underscore (_) and the dollar ($) sign. For example,
var name = ‘Sam’;

var $name = ‘Pam’;

int number = 4.4;
  1. Variable names cannot contain keywords. For example,
var void = ‘Sam’;
  1. Variable names should be alphabet and cannot start with numbers. For example,
var 2name = ‘Sam’;
  1. Dart is case-sensitive. Hence a and A are different. For example,
var a = ‘Sam’;

var A = ‘Smith’;

A is not equal to a.


Dart Constants

Dart constants are similar to the dart variables. They are used to store values. The only difference is that the values of a constant cannot be changed afterwards.

The constants are denoted by the keyword final and const. For example,

final name = ‘Sam’;

const number = 500;

We will learn about Dart Operators in the next chapter.

Was this helpful?

1 Yes  2 No
Related Articles
  • Dart Constructors
  • Dart Exceptions
  • Dart Objects
  • Dart Classes
  • Control Flow
  • Dart Functions
Previously
Getting Started with Dart
Up Next
Dart Operators
flutter-dev-experts




FLUTTERGUIDE
FlutterGuide is an easy learning guide platform to help you build apps for any screen. The leading Dart/Flutter guides for getting started and become pro.
Guides
  • Getting started with Dart 22
  • Private Documentation 1
Categories
  • Firebase
  • Dart
  • iOS
  • API
  • Bloc
  • Flutter
  • Widgets
  • Mobile
  • State Management
  • Design
  • Animations
  • Flutter Designs
About
About Us
Guides
Blogs
FAQs
Contact
  • Privacy Policy
  • Terms & Conditions
  • Copyright 2022 Fawesome Apps. All Rights Reserved