Variables
Here’s an example of creating a variable and initializing it:
var name = 'Bob';
Variables store references. The variable called name
contains areference to a String
object with a value of “Bob”.
The type of the name
variable is inferred to be String
,but you can change that type by specifying it.If an object isn’t restricted to a single type,specify the Object
or dynamic
type, followingdesign guidelines.
dynamic name = 'Bob';
Another option is to explicitly declare the type that would be inferred:
String name = 'Bob';
Note: This page follows the style guide recommendation of using var
, rather than type annotations, for local variables.