Created
July 10, 2018 19:13
-
-
Save aab29/3946ab2083f0023991d391d571f9e995 to your computer and use it in GitHub Desktop.
An example of how concise you can make your constructor code in Dart
This file contains hidden or 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
| class City { | |
| String name; | |
| String countryName; | |
| int population; | |
| bool isCapital; | |
| City(this.name, this.countryName, this.population, {this.isCapital = false}); | |
| @override | |
| String toString() => "The city of $name, $countryName has a population of $population."; | |
| } | |
| void main() { | |
| var cities = [ | |
| new City("New York", "USA", 8537673), | |
| new City("Beijing", "China", 21516000, isCapital: true), | |
| new City("Shanghai", "China", 24256800), | |
| new City("São Paulo", "Brazil", 12038175), | |
| new City("Cairo", "Egypt", 10230350, isCapital: true) | |
| ]; | |
| for (var city in cities) { | |
| print(city); | |
| if (city.isCapital) { | |
| print("${city.name} is a capital city!"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment