DEV Community

Geoffrey Kim
Geoffrey Kim

Posted on

1

Understanding Variables in Dart: A Comprehensive Guide

The Var Keyword

  1. var
    • There is no need to specify the variable's type.
    • When updating the value, it must match the original type of the variable.
  2. Explicit Declaration
    • Conventionally, the var keyword is used to declare local variables within functions or methods (recommended by the Dart style guide, as the compiler knows the type anyway).
    • When declaring variables or properties in a class, the type is specified.

Dynamic Type

  • A keyword used for variables that can have various types.
  • It's not generally recommended, but it can be very useful at times.
  • Why? It is used when it is difficult to know what type a variable will be, especially when working with Flutter or JSON.
  • Example of usage:
void main() {
  var a;
  dynamic b;
}
Enter fullscreen mode Exit fullscreen mode

Nullable Variables

Null safety prevents the developer from referencing null values.
In Dart, it must be clearly indicated whether a variable can be null.

void main() {
  String? nico = 'nico';
  nico = null;
  nico?.isNotEmpty; // This checks if the value exists before proceeding with the following operations. Equivalent to the below.
  if (nico != null) {
    nico.isNotEmpty;
  }
}
Enter fullscreen mode Exit fullscreen mode

Final Variables

To define a variable that cannot be modified, use final instead of var.
It's similar to const in JavaScript or TypeScript.

void main() {
  final a = 'nico';
  final String b = 'nico';
}
Enter fullscreen mode Exit fullscreen mode

Late Variables

late is a modifier that can be added before final or var.
late allows declaring a variable without initial data.

void main() {
  late final String name;
  // do something, go to api
  name = 'nico';
}
Enter fullscreen mode Exit fullscreen mode

Constant Variables

const in Dart is different from JavaScript or TypeScript.
In JavaScript or TypeScript, const is similar to final in Dart.
In Dart, const is used to create compile-time constants.
It's used for values that are known at compile time.
These values are known before uploading the app to the app store.

void main() {
  const name = 'nico';
  const max_allowed_price = 120;
}
Enter fullscreen mode Exit fullscreen mode

Recap

According to Dart's style guide, it is recommended to use var as much as possible, and using types is advised when writing properties in a class. When declaring local variables within methods or small functions, using var is preferable since the compiler will infer the variable's type anyway.

The dynamic type must be used very cautiously.

References

https://nomadcoders.co/dart-for-beginners

Image of Timescale

Timescale โ€“ the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

๐Ÿ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay