Last active
July 3, 2019 08:18
-
-
Save nikli2009/538fbc62cfcc69e8ae0033a6129b71b8 to your computer and use it in GitHub Desktop.
Dart Const / Final
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
void main() { | |
const String USER_NAME = 'nikk'; | |
// USER_NAME = 'wenyan'; => Error | |
final String USER_ADDRESS = 'China'; | |
// USER_ADDRESS = 'United States'; => Error | |
// ------------------------------------ | |
// Runtime Value Assignment | |
// const int USER_AGE = gen(); | |
// print('const - $USER_AGE'); => Error | |
final int AMY_ADDRESS = gen(); | |
print('final - $AMY_ADDRESS'); // => Works | |
// try assign new value for final | |
// AMY_ADDRESS = gen(); => Error | |
} | |
int gen() { | |
return Random().nextInt(5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment