kota's memex

declaration

var

Declares a variable without specifying its type.
var num = 42
Type int is determined by the initial value of 42.

objects

Everything you can place in a variable is an object, and every object is an instance of a class. Even numbers, functions, and null are objects. With the exception of null (if you enable sound null safety), all objects inherit from the Object class.

null safety

If you enable null safety, variables can’t contain null unless you say they can. You can make a variable nullable by putting a question mark (?) at the end of its type. For example, a variable of type int? might be an integer, or it might be null. If you know that an expression never evaluates to null but Dart disagrees, you can add ! to assert that it isn’t null (and to throw an exception if it is). An example: int x = nullableButNotNullInt!

any type allowed

When you want to explicitly say that any type is allowed, use the type Object? (if you’ve enabled null safety), Object, or — if you must defer type checking until runtime — the special type dynamic.

generic types

Dart supports generic types, like List<int> (a list of integers) or List<Object> (a list of objects of any type).

final and const

If you never intend to change a variable, use final or const, either instead of var or in addition to a type. A final variable can be set only once; a const variable is a compile-time constant. (Const variables are implicitly final.)

Note: Instance variables can be final but not const.

final name = 'Bob'; // Without a type annotation
final String nickname = 'Bobby';