Skip to content

Instantly share code, notes, and snippets.

@bernbecht
Created June 29, 2021 07:15
Show Gist options
  • Save bernbecht/6cde6e080d7193cda2285d937ac846b9 to your computer and use it in GitHub Desktop.
Save bernbecht/6cde6e080d7193cda2285d937ac846b9 to your computer and use it in GitHub Desktop.
class Person {
// String and int variables don't allow `null`
// They wanna be initialized
// `late` says to compiler to not worry as these vars will be init later
late String name;
late int age;
// constructor with named parameters, with default values
Person({String inputName = '', int age = 30}) {
name = inputName;
// if parameter and class field has same name, you need to use `this.`
this.age = age;
}
// constructor with sequence parameters
// Person(String inputName, int inputAge) {
// this.name = inputName;
// this.age =age
// }
// short hand constructor
// Person({this.name, this.age = 30});
}
void main () {
var p1 = Person(inputName: 'Max', age: 31);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment