Created
May 4, 2019 15:12
-
-
Save codediodeio/210b8f780a8acd08ae981b00bbec8e93 to your computer and use it in GitHub Desktop.
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
class Dog { | |
String breed; | |
int age; | |
Dog({ this.age, this.breed }); | |
factory Dog.fromMap(data) { | |
return Dog( | |
breed: data['breed'], | |
age: data['age'], | |
); | |
} | |
} | |
// Map the class name to the constructor | |
Map models = { | |
Dog: (data) => Dog.fromMap(data) | |
}; | |
// Database service | |
class Data<T> { | |
T fetch() { | |
Map data = { 'breed': 'pug', 'age': 10 }; | |
return models[T](data); | |
} | |
} | |
void main() { | |
Data<Dog> dogRef = Data(); | |
Dog dog = dogRef.fetch(); | |
print(dog.breed); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment