Skip to content

Instantly share code, notes, and snippets.

@aab29
Created July 10, 2018 19:13
Show Gist options
  • Select an option

  • Save aab29/3946ab2083f0023991d391d571f9e995 to your computer and use it in GitHub Desktop.

Select an option

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
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