Difference between var and other more specific types in dart

318

Solution 1

You never need to use var; you always can choose to specify an explicit type instead.

var is used if you want to declare a variable and want the type to be inferred from the initializer. (If there is no initializer, the variable will be of type dynamic. However, since dynamic disables static type checking for that variable and incurs an extra runtime cost, you should avoid dynamic when possible and should explicitly variables as dynamic when it's necessary.

If the variable type is inferrable, whether you use implicit or explicit types is a matter of style.

Advantages of using explicit types:

  • Can be more readable without tooling (i.e., without IDE support that can show you variable types).
  • Can catch type errors where the inferred type isn't the expected type.

Advantages of using implicit types:

  • Less verbose, which arguably makes the rest of the code more readable.
  • Can avoid type errors where the explicit type is accidentally less precise than the inferred type. For example: List someList = [1, 2, 3]; actually declares someList to be of type List<dynamic> even though the right-hand-side is inferred to be List<int>.

Solution 2

You have

  • var Multiple asignment and Dynamic type. It can be assigned from expressions of different types. Be careful, Dart will infer the correct type, as long as variables are declared and initialized at the same time if you are using var.

  • final Single assignment. It is often used for when declaring properties inside widget classes.

  • const Compile time constant.

  1. Is used to define hard-coded values, such as colors, font sizes and icons.
  2. Used as a constructor when you're defining widget classes Const as a constructor, the widget is optimised by Flutter, and it isn't rebuilt when the parent changes.

Prefer const over final when possible.

Solution 3

Dart is a statically typed language so Dart needs to be able to determine the type at some point. But it is also smart enough to automatically determine the type based on the context. E.g. the type of the returned value from a function.

It is really a question about style but personally I think the type should be specified for method signatures and class variables. For all variables inside a method, I will use var/final and use clear variable names.

Another reason I try use var/final as much as possible is this can prevent some nasty issues with e.g. generics (List list = <String>['test'] makes list variable the type List<dynamic>.

dynamic in Dart is really just saying to the compile "just let this pass and check if the program makes sense at runtime". So we really don't want to have much dynamic in our code base since we are then using the compilers ability to statically check your program makes sense.

Share:
318
Christopher Lee
Author by

Christopher Lee

Updated on December 28, 2022

Comments

  • Christopher Lee
    Christopher Lee over 1 year

    I come from Javascript's "let", and I am a bit confused on what the big differences are between var and other types. I understand that there are some cases where we need to use var(anonymous data types), and some where we need to use explicit types. I also understand that the var is less concise and the types are more, but is there a more in depth or practical application of using types instead of var within Flutter development?