1

Wanted to create an interface for DAOs that can be "overloaded" with different method params types:

abstract class DaoInterface {
  Future<List<Object?>> findAll();
  Future<Object?> findOne(int id);
  Future<Object?> insert<T>(T value);
  Future<Object?> update(Object dto);
  Future<Object?> deleteOne(Object dto);
  Future<Object?> deleteAll();
}

// impl
class CategoryDAO implements DaoInterface {
@override
Future<CategoryEntity?> deleteAll() {
  // TODO: implement deleteAll
  throw UnimplementedError();
}

@override
Future<CategoryEntity?> deleteOne(CategoryEntity category) {
  // TODO: implement deleteOne
  throw UnimplementedError();
}

@override
Future<List<CategoryEntity?>> findAll() {
  // TODO: implement findAll
  throw UnimplementedError();
}

@override
Future<CategoryEntity?> findOne(int id) {
  // TODO: implement findOne
  throw UnimplementedError();
}

@override
Future<CategoryEntity?> update(CategoryEntity category) {
  // TODO: implement update
  throw UnimplementedError();
}

@override
Future<CategoryEntity?> insert(CategoryEntity entity) {
  // TODO: implement insert
  throw UnimplementedError();
}

}

the above throws warnings for insert, update, deleteOne methods, ie:

'CategoryDAO. insert' ('Future<CategoryEntity?> Function(CategoryEntity)') isn't a valid override of 'DaoInterface. insert' ('Future<Object?> Function(T)'). (Documentation) The member being overridden (dao_interface. dart:9).

I've tried using Object base class, generics, etc- and nothign seems to allow this behavior. I expected to implement this interface for other DAOs that take different entity classes to reduce code duplication (PersonDAO using PersonEntity, etc). Thoughts? Am I approaching this incorrectly?

1 Answer 1

1

The Dart analyzer displays an error message because:

The parameter of an overridden method must have either the same type or a supertype of the corresponding parameter in the superclass.

For more info see use-sound-parameter-types-when-overriding-methods.

If there is good reason to use a subtype as a function parameter you can do that by using the keyword covariant:

@override
Future<CategoryEntity?> insert(covariant CategoryEntity entity) {
  // TODO: implement insert
  throw UnimplementedError();
}

Another option would be to change DaoInterface to:

// Note: DaoInterface is now a generic class. 
abstract class DaoInterface<T extends Object> {
  Future<List<T?>> findAll();
  Future<T?> findOne(int id);
  Future<T?> insert(T value);
  Future<T?> update(T dto);
  Future<T?> deleteOne(T dto);
  Future<T?> deleteAll();
}

class CategoryEntity {}

// Note: Uses CategoryEntity as type parameter of DaoInterface. 
class CategoryDAO implements DaoInterface<CategoryEntity> {
  @override
  Future<CategoryEntity?> deleteAll() {
    // TODO: implement deleteAll
    throw UnimplementedError();
  }

  @override
  Future<CategoryEntity?> deleteOne(CategoryEntity category) {
    // TODO: implement deleteOne
    throw UnimplementedError();
  }

  @override
  Future<List<CategoryEntity?>> findAll() {
    // TODO: implement findAll
    throw UnimplementedError();
  }

  @override
  Future<CategoryEntity?> findOne(int id) {
    // TODO: implement findOne
    throw UnimplementedError();
  }

  @override
  Future<CategoryEntity?> update(CategoryEntity category) {
    // TODO: implement update
    throw UnimplementedError();
  }

  @override
  Future<CategoryEntity?> insert(CategoryEntity entity) {
    // TODO: implement insert
    throw UnimplementedError();
  }
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.