Skip to content

Instantly share code, notes, and snippets.

@kranfix
Last active January 9, 2022 17:24
Show Gist options
  • Save kranfix/43e20c9617c7be40b750ba0b638d289c to your computer and use it in GitHub Desktop.
Save kranfix/43e20c9617c7be40b750ba0b638d289c to your computer and use it in GitHub Desktop.
Problem using `var` in Dart
void main() {
final list = <int>[1, 2, 3, 4, 2, -1, 5];
final maxValue = 12;
var sum = 0;
for(final val in list) {
sum += val;
}
if(sum > maxValue) {
sum = maxValue;
}
// The code can use `sum` but allowing the mutation.
// It can drive developers to unintentional errors.
/// ... more code that uses `sum` ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment