Skip to content

Instantly share code, notes, and snippets.

@codediodeio
Created May 4, 2019 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codediodeio/210b8f780a8acd08ae981b00bbec8e93 to your computer and use it in GitHub Desktop.
Save codediodeio/210b8f780a8acd08ae981b00bbec8e93 to your computer and use it in GitHub Desktop.
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