What is the proper way to get a metadata of the type in languages that doesn't allow static methods/functions to be overridden? For example, I'm using Dart:

abstract class Model {
  // Constructor
  final String id;

class MyModel extends Model {
  // Constructor
  MyModel(String id, this.name) : super(id);
  final String name;

class FirebaseRepository<T extends Model> extends Repository {
  Future<List<T>> fetchList() {
    // How to get `mymodels` value knowing T??
    return _firestore.collection('mymodels').get();

What I want is to store and retrieve a collection path (table name) for my MyModel model. The value is mymodels.

The easiest way is to declare a static func in Model and implement it in subclasses. But the problem is Dart doesn't allow to inherit and override static members.

Another options is to create a func that returns collection path based on type passed into it. I don't like that solution as it requires to use reflection and it is a code smell.

What are other options?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.