void Function(Subype) isn't a valid override of void Function(ParentType)

Viewed 235

I'm trying to create a parent type (IDataProvider) and a subtype (ILocalDataProvider), and then in a class, specify the subtype to take the place of the parent type. Here is what I mean:

abstract class IDataProvider {
  ValueType get<ValueType>(String keyToRead);
}

abstract class ILocalDataProvider extends IDataProvider {
  bool isKeyExistent(String key);
}

abstract class IDataSource {
  IDataProvider dataProvider;
}

class LocalDataSource implements IDataSource {
  LocalDataSource({@required this.dataProvider});

  @override
  ILocalDataProvider dataProvider; //*************** ERROR ON THIS LINE **********//
}

I'm getting this error:

'LocalDataSource.dataProvider=' ('void Function(ILocalDataProvider)') isn't a valid override of 'IDataSource.dataProvider=' ('void Function(IDataProvider)').

I thought that since ILocalDataProvider is a subtype of IDataProvider, it will be recognised as an IDataProvider?? What am I doing wrong?

1 Answers

You can't do that. The functions' signatures must match exactly. However, you can use a generic parameter like this: abstract class IDataSource<ProviderType extends IDataProvider> {}

Related