DEV Community

Cover image for Brief Summary : Dart Variable
swimmingkiim
swimmingkiim

Posted on • Edited on

3 2

Brief Summary : Dart Variable

Image description
This post may helpful for those who need quick remind of Dart grammar, or a programmer from a different language learning Dart.

Basic

Variables store references.

var

var language = 'dart';
Enter fullscreen mode Exit fullscreen mode
  • Mutable variable keyword

By Type

String language = 'dart';
Enter fullscreen mode Exit fullscreen mode
  • You can specify the type in order to declare variable.

final & const

final fruits = {
    1: 'apple',
    2: 'orange'
};
const animals = {
    1: 'dog',
    2: 'cat'
};

fruits[1] = 'banana'; // NO ERROR
animals[1] = 'monkey'; // ERROR
Enter fullscreen mode Exit fullscreen mode
  • There’re similar in sense that they can’t be changed once you assign a value to them.

The Difference Between “final” and “const” keyword

  • final can’t be changed once you assign a value.
  • const is also like that, but in advance, they require more rules
    • The value that you’re going to assign should be a compile-time constant
      • compile-time constant means a constant value that is already fixed by the time when dart compile process.
      • For example, String literals that dose not contain interpolated expression and expression must be one of these(null, numeric, string, or boolean)
  • Also, after assigning a value, variables annotated with final can’t be reassigned, but it’s fields can.
  • Unlike final, variables annotated with const can’t be reassigned, both itself ant it’s sub fields.

late

late String onlyKnowAfterInitialization;

onlyKnowAfterInitialization = 'hello world';
Enter fullscreen mode Exit fullscreen mode
  • Keyword that’s used in below cases.
    • You need to assign a value after you declare it.
    • You want to use lazy assignment.
      • For example, when you need to access this in order to assign a value.
  • If the value of a variable annotated with late keyword needs to be computed, the computation will not happen unless the variable is actually used in somewhere.

Cheers!

Buy Me A Coffee

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay