Skip to content

Instantly share code, notes, and snippets.

@fvisticot
Last active October 3, 2019 21:49
Show Gist options
  • Save fvisticot/332eb3ae0e755db1f8fb197b3879b699 to your computer and use it in GitHub Desktop.
Save fvisticot/332eb3ae0e755db1f8fb197b3879b699 to your computer and use it in GitHub Desktop.
Dart contructors
void main() {
Book book = Book(title: 'bookTitle', description: 'bookDescription');
Book book2 = Book.fromMap({'title': 'Book2', 'description': 'desc2'});
print(book);
print(book2);
SuperBook superBook = SuperBook(title: "superBookTitle", description: "superBookDesc", size: 10);
print(superBook);
Author author1 = Author('fred', 'visticot');
Author author2 = Author('john', 'smith', '0674639362');
Author author3 = Author.custom('johsn', 'lorient', tel: '0202020');
print(author1);
print(author2);
print(author3);
print(author1._getName());
}
class Author {
final String firstname;
final String lastname;
final String tel;
final String email;
String _getName() {
return 'fred';
}
Author(this.firstname, this.lastname, [this.tel, this.email]);
Author.custom(this.firstname, this.lastname, {this.tel, this.email});
String toString() {
return '$firstname, $lastname, $email, $tel';
}
}
class Book {
final String title;
final String description;
Book({this.title, this.description});
factory Book.fromCache(Map<String, dynamic> map) {
return Book(title: map['title'], description: map['description']);
}
Book.fromMap(Map<String, dynamic> map) : this.title= map['title'], this.description=map['description'];
@override
String toString() {
return '$title, $description';
}
}
class SuperBook extends Book {
final int size;
SuperBook({String title, String description, this.size}): super(title: title, description: description);
@override
String toString() {
return '$title, $description $size';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment