Variables in Dart

Variables in Dart

Variable is a named space in memory that stores value. You can say that it is a container for data.

·

3 min read

Variable declaration and its fundamentals are the basic building block of any programming language. Let’s explore a little bit about dart variables.

Variable is a named space in memory that stores value. You can say that it is a container for data.

The naming rule for Dart Variables

  • Identifiers cannot be keywords.

  • Identifiers can contain alphabet and numbers. But an identifier cannot start with a number.

  • Identifiers cannot contain white spaces and special characters except the underscore (_) and the dollar ($) sign.

Variable declaration Syntax

  • In Dart, a variable must be declared before it is used.

  • Example of creating and initializing the variable.

var name = “Akshay”;
  • Here var is a keyword used to declare a variable.

  • Dart supports a type-inference. So that here compiler will automatically know that this is a variable of type string by the initializer.

  • Here you can explicitly specify the type of a variable by prefixing it. Consider below example:

String name = "Akshay";
int highScore = 120;

All the variables in Dart store a reference to the value rather than containing a value. Here variable name contains a reference to a string object with a value of “Shivansh”.

The Dynamic Type

  • Variables declared without a static type are implicitly dynamic.

  • If a variable is not restricted to a single type, specify it as a dynamic type.

dynamic score = 56;

Default Values

All uninitialized variables have an initial value of null*.*

Even variables with numeric types are initially null because numbers — like everything else in Dart — are objects.

void main() {
  int highScore;
  print(highScore);
}

Output
null

Conclusion

In conclusion, variables are essential building blocks in Dart programming. They serve as named memory spaces that store values. Dart follows specific naming rules for variables, excluding keywords, starting with letters, and allowing only certain special characters. Variables must be declared before use, and Dart supports type inference for automatic type determination. Additionally, uninitialized variables have a default value of null. Understanding these fundamental concepts will enable you to write efficient and effective Dart code.

That’s all you need to know about the basics of variables in the dart. Feel free to let me know if I miss something. I’ll love to learn it.

Jai Hind, Vande Mataram 🇮🇳

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitant that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

💡
Subscribe To My Newsletter For More Such Content.

Learn More about Dart and Flutter

Follow me for more such content

Did you find this article valuable?

Support Jay Tillu by becoming a sponsor. Any amount is appreciated!