Skip to content

Instantly share code, notes, and snippets.

@iAmWillShepherd
Created December 7, 2021 17:13
Show Gist options
  • Save iAmWillShepherd/8236262117dd9a4c6250c06ef90c51da to your computer and use it in GitHub Desktop.
Save iAmWillShepherd/8236262117dd9a4c6250c06ef90c51da to your computer and use it in GitHub Desktop.
Comparing and constrasting Dart's const and final keywords

Use the const keyword when you want to create a compile-time constant.
Note that this value will be immutable.

const color = 'Lava Orange';
color = 'Mexico Blue';  // Error: cannot reassign a const

const colors = ['Lava Orange'];
colors.add('Lava Orange');  // Error: unsupported operation

Use the final keyword when you want to create a readonly variablae. run-time constant. Note that you may change properties of the value being pointed to, but you can't change the pointer.

final color = 'Lava Orange';
color = 'Mexico Blue';  // Error: cannot reasign a final

const colors = ['Lava Orange'];
colors.add('Mexico Blue');  // [Lava Orange, Mexico Blue]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment